Skip to content

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

Merged
merged 17 commits into from
Feb 25, 2023
Merged

Conversation

zmievsa
Copy link
Contributor

@zmievsa zmievsa commented Feb 24, 2023

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.

@github-actions

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a 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?

@github-actions

This comment has been minimized.

@zmievsa
Copy link
Contributor Author

zmievsa commented Feb 24, 2023

I should've used pre-commit here all along but I guess it's lesson learned.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@AlexWaygood
Copy link
Member

I should've used pre-commit here all along but I guess it's lesson learned.

No worries at all! The reason we have autofixes turned on in CI is so that people don't have use pre-commit locally :)

Comment on lines 165 to 166
@overload
def get_origin(tp: ParamSpecArgs | ParamSpecKwargs) -> ParamSpec: ...
Copy link
Member

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 ParamSpecs 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'>

Copy link
Contributor Author

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!

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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]

@AlexWaygood AlexWaygood merged commit 806b134 into python:main Feb 25, 2023
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.

3 participants