-
Notifications
You must be signed in to change notification settings - Fork 20
context_management: add a type stub override to fix typing #457
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
context_management: add a type stub override to fix typing #457
Conversation
|
Build succeeded. ✔️ pre-commit SUCCESS in 1m 54s |
nforro
left a comment
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.
This is great, thanks!
Could you just add the copyright header to the new file?
|
/packit build |
|
/packit-stg build |
|
/packit rebuild-failed |
|
/packit-stg rebuild-failed |
Type checkers (mypy, pyright, et al.) don't understand the
ContextManager descriptor class.
With a stub file, we can tell the type checker to treat ContextManager
as a simple decorator function which is something that it understands.
``` python
from typing import reveal_type
import specfile
s = specfile.Specfile("./fedora/python-specfile.spec")
\# Before: types.MethodType
\# After: (allow_duplicates: bool = False, default_to_implicit_numbering: bool = False, default_source_number_digits: int = 1) -> GeneratorContextManager[Sources]
reveal_type(s.sources)
\# Before: Any
\# After: GeneratorContextManager[Sources]
reveal_type(s.sources())
\# Before: Any
\# After: Sources
reveal_type(s.sources().__enter__())
reveal_type(s.sources().content)
```
a604ec9 to
51565f1
Compare
|
Build succeeded. ✔️ pre-commit SUCCESS in 1m 52s |
|
/packit build |
|
/packit-stg build |
|
regate |
|
Build succeeded (gate pipeline). ✔️ pre-commit SUCCESS in 1m 59s |
eab3524
into
packit:main
context_management: use ParamSpec from typing_extensions Python < 3.10 does not have ParamSpec, so we need to import it from typing_extensions. Fixes #457 RELEASE NOTES BEGIN The context_management type stubs now use ParamSpec from typing_extensions to support Python < 3.10. RELEASE NOTES END Reviewed-by: Nikola Forró Reviewed-by: Maxwell G
Type checkers (mypy, pyright, et al.) don't understand the ContextManager descriptor class.
With a stub file, we can tell the type checker to treat ContextManager as a simple decorator function which is something that it understands.
RELEASE NOTES BEGIN
context_management: add a type stub override to fix typing. Type checkers like mypy and pyright can now correctly determine the types for
.sources(),.sections(), and the otherSpecfilemethods that return context managers.RELEASE NOTES END