Commit eab3524
context_management: add a type stub override to fix typing (#457)
context_management: add a type stub override to fix typing
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.
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)
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 other Specfile methods that return context managers.
RELEASE NOTES END
Reviewed-by: Nikola Forró2 files changed
+27
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
0 commit comments