-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add get_origin annotations #9811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Could you make the same changes in typing.pyi
?
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This comment has been minimized.
This comment has been minimized.
…peshed into add-get-origin-annotations
…peshed into add-get-origin-annotations
…peshed into add-get-origin-annotations
I should've used pre-commit here all along but I guess it's lesson learned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
No worries at all! The reason we have autofixes turned on in CI is so that people don't have use pre-commit locally :) |
stdlib/typing_extensions.pyi
Outdated
@overload | ||
def get_origin(tp: ParamSpecArgs | ParamSpecKwargs) -> ParamSpec: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, actually, this overload can work on 3.7+ for typing_extensions
(but not for typing
, where ParamSpec
s only exist on 3.10+):
(py37) C:\Users\alexw\coding\typeshed>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing_extensions import ParamSpec, get_origin
>>> type(get_origin(ParamSpec("P").args))
<class 'typing_extensions.ParamSpec'>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will take care of it later today. Thanks!
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: pydantic (https://github.com/samuelcolvin/pydantic)
+ pydantic/_internal/_generate_schema.py:377: error: Unused "type: ignore" comment
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ src/hydra_zen/structured_configs/_utils.py:69: error: Name "get_origin" already defined (possibly by an import) [no-redef]
- src/hydra_zen/structured_configs/_utils.py:69: error: All conditional function variants must have identical signatures [misc]
- src/hydra_zen/structured_configs/_utils.py:69: note: Original:
- src/hydra_zen/structured_configs/_utils.py:69: note: def get_origin(tp: Any) -> Optional[Any]
- src/hydra_zen/structured_configs/_utils.py:69: note: Redefinition:
- src/hydra_zen/structured_configs/_utils.py:69: note: def get_origin(tp: Any) -> Optional[type]
|
Rationale
When you know for a fact that you are using get_origin on a GenericAlias, your type checker will show you errors about the result being
Any | None
. These type hints attempt to fix this issue at least for the most common use cases. I tried handling every overload that was possible to handle but if you have any suggestions -- please, share them :)CI seems to be broken because not every import is available in earlier versions. Am thinking on how to handle it right now.