Prioritize .pyi from -stubs packages over bundled .pyi#19001
Prioritize .pyi from -stubs packages over bundled .pyi#19001JelleZijlstra merged 3 commits intopython:masterfrom
.pyi from -stubs packages over bundled .pyi#19001Conversation
This comment has been minimized.
This comment has been minimized.
| dir_prefix = os.path.dirname(dir_prefix) | ||
|
|
||
| # Stubs-only packages always take precedence over py.typed packages | ||
| path_stubs = f"{base_path}-stubs{sepinit}.pyi" |
There was a problem hiding this comment.
hm this assumes there is an __init__.pyi, I don't think that's required?
There was a problem hiding this comment.
It's pretty much the same as the path_stubs = base_path + "-stubs" + sepinit + extension from before before 🤷🏻
Should I change it to something else?
There was a problem hiding this comment.
Yea you're right, apparently this isn't the whole story:
I added some new tests for a.py[i], but those are failing.
There was a problem hiding this comment.
Hm somehow the tests only fail if it's called a.pyi, and not when called spam.pyi. I'm guessing it's a caching thing. Either way, it's probably out out scope, and might be limited to these unit-tests.
There was a problem hiding this comment.
hm this assumes there is an
__init__.pyi, I don't think that's required?
See the updated PR description
There was a problem hiding this comment.
Yeah I believe namespace packages are handled on L525 afterwards.
|
Diff from mypy_primer, showing the effect of this PR on open source code: porcupine (https://github.com/Akuli/porcupine)
- porcupine/plugins/highlight/tree_sitter_highlighter.py:49: error: "Parser" has no attribute "set_language"; maybe "language"? [attr-defined]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:84: error: Item "None" of "bytes | None" has no attribute "decode" [union-attr]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:95: error: Item "None" of "Node | None" has no attribute "start_point" [union-attr]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:96: error: Item "None" of "Node | None" has no attribute "end_point" [union-attr]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:101: error: Item "None" of "Node | None" has no attribute "type" [union-attr]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:103: error: Argument 1 to "captures" of "Query" has incompatible type "Node | None"; expected "Node" [arg-type]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:112: error: Unpacking a string is disallowed [misc]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:113: error: Cannot determine type of "tag_or_recurse" [has-type]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:114: error: Cannot determine type of "subnode" [has-type]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:116: error: Cannot determine type of "subnode" [has-type]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:116: error: Cannot determine type of "tag_or_recurse" [has-type]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:126: error: Item "None" of "Node | None" has no attribute "type" [union-attr]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:132: error: Incompatible types in "yield" (actual type "tuple[Node | None, str]", expected type "tuple[Node, str]") [misc]
- porcupine/plugins/highlight/tree_sitter_highlighter.py:132: error: Argument 1 to "_decide_tag" of "TreeSitterHighlighter" has incompatible type "Node | None"; expected "Node" [arg-type]
|
emmatyping
left a comment
There was a problem hiding this comment.
Thank you for the fix! Note that the mypy docs do not supersede PEP 561, but rather the typing specification now contains the module resolution order: https://typing.python.org/en/latest/spec/distributing.html#mro
Oh woops, I meant to reference the typing spec there. |
This fixes the import resolution order for stubs from a
-stubspackage and stubs bundled with apy.typedpackage, and fixes #18997.Besides the unit tests, the effectiveness of this fix is also demonstrated at https://github.com/jorenham/mypy-pep561-numpy-issue#mypy-jorenhamfix-18997-with-numtype
After investigating a bit more, it looks like mypy's incorrect prioritization of stubs was limited to
__init__.pyi. I confirmed this by addingreveal_type(np.dtypes.StringDType())to themain.pyiin https://github.com/jorenham/mypy-pep561-numpy-issue. Withmypy==1.15.0installed, it correctly showednumpy.dtypes.StringDTypewithout NumType, andnumpy.dtypes.StringDType[Never]with NumType installed. So both #18997 and this PR only apply to__init__.pyi.