Skip to content

Conversation

@martynia
Copy link
Contributor

@martynia martynia commented May 6, 2025

No description provided.

@martynia martynia force-pushed the janusz_main_repo_test_mypy branch from 3efc6fa to 1477501 Compare July 1, 2025 08:51
@martynia
Copy link
Contributor Author

martynia commented Jul 1, 2025

def _apply_default_scheme(value: str) -> str:
 """Applies the default git+file:// scheme if not present."""
 if isinstance(value, str) and "://" not in value:
     value = f"git+file://{value}"
 return value


class AnyUrlWithoutHost(AnyUrl):
    _constraints = UrlConstraints(host_required=False)


ConfigSourceUrl = Annotated[AnyUrlWithoutHost, BeforeValidator(_apply_default_scheme)]

@aldbr MyPy complains about code listed above because value is declared as str which makes isinstance test redundant. Pydantic manual hints that types in this case should be generic (they have Any). Then we could extend the test but this doesn't work for us, because AnyUrl scheme attribute is read-only:

elif isinstance(value, AnyUrl):
     value.scheme = "git+file" 

If Any, the validator would let in AnyUrl with any scheme which IMHO denies its purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant