-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
improve inspect.pyi #5473
improve inspect.pyi #5473
Conversation
JelleZijlstra
commented
May 16, 2021
- Use TypeGuard for various is* functions (refer to PEP 647 (TypeGuard) tracker #5406)
- Use collections.abc and builtin containers
- Use TypeGuard for various is* functions (refer to python#5406) - Use collections.abc and builtin containers
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.
LGTM, except for pyright's warning and the marked spot.
stdlib/inspect.pyi
Outdated
|
||
# Create private type alias to avoid conflict with symbol of same | ||
# Create private type alias to avoid confdict with symbol of same |
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.
^^
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.
Oops, thanks
Looks like mypy crashes on |
This came up in python/typeshed#5473: mypy started crashing when I made a function return TypeGuard[Type[Any]]. I wasn't able to reproduce this locally so far, but verified that removing the assertion fixes the crash.
Primer still fails to leave a comment. |
I reset the permissions at https://github.com/python/typeshed/settings/actions and restarted the CI run. |
And since that had no effect, I set the stricter permissions again. I'm trying something else. |
🤞 |
This comment has been minimized.
This comment has been minimized.
Well, it worked! But now i need to figure out what all those changes are about. For reference: https://github.com/sphinx-doc/sphinx/blob/4.x/sphinx/util/inspect.py#L252, https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/elements/doc_string.py#L105 Looks like mypy has some bug around a TypeGuard anded with another if check. |
This came up in python/typeshed#5473: mypy started crashing when I made a function return TypeGuard[Type[Any]]. I wasn't able to reproduce this locally so far, but verified that removing the assertion fixes the crash.
In python/typeshed#5473, I tried to switch a number of `inspect` functions to use the new `TypeGuard` functionality. Unfortunately, mypy-primer found a number of crashes in third-party libraries in places where a TypeGuard function was ANDed together with some other check. Examples: - https://github.com/sphinx-doc/sphinx/blob/4.x/sphinx/util/inspect.py#L252 - https://github.com/sphinx-doc/sphinx/blob/4.x/sphinx/ext/coverage.py#L212 - https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/elements/doc_string.py#L105 The problems trace back to the decision in #9865 to make TypeGuardType not inherit from ProperType: in various conditions that are more complicated than a simple `if` check, mypy wants everything to become a ProperType. Therefore, to fix the crashes I had to make TypeGuardType a ProperType and support it in various visitors.
Made this include #5551 to see what happens with mypy-primer. |
This comment has been minimized.
This comment has been minimized.
1 similar comment
Diff from mypy_primer, showing the effect of this PR on open source code: sphinx (https://github.com/sphinx-doc/sphinx.git)
+ sphinx/util/inspect.py: note: In function "is_singledispatch_function":
+ sphinx/util/inspect.py:334:13: error: "FunctionType" has no attribute "dispatch"
+ sphinx/util/inspect.py: note: At top level:
+ sphinx/ext/coverage.py: note: In member "build_py_coverage" of class "CoverageBuilder":
+ sphinx/ext/coverage.py:218:51: error: Item "object" of "Union[object, object]" has no attribute "__doc__"
graphql-core (https://github.com/graphql-python/graphql-core.git)
+ src/graphql/subscription/subscribe.py:89: error: unused "type: ignore" comment
+ tests/execution/test_sync.py:61: error: Redundant cast to "Awaitable[Any]"
|
The coverage.py one is still a bug in mypy (https://github.com/sphinx-doc/sphinx/blob/4.x/sphinx/ext/coverage.py#L218). It somehow narrows the type down from Any to object. |
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.
The impact of the mypy bug seems to be rather small. The problem with inspect.py
is unfortunate, but a standard problem with mypy not understanding hasattr()
.
Other two type ignores here are pending update of typeshed in mypy: python/typeshed#5473
Other two type ignores here are pending update of typeshed in mypy: python/typeshed#5473
* Work around a MyPy inference (? or typeshed) bug * Clean up CacheMappingView * A few more type ignores * Please flake8 * Revert isinstance checks * Use TypeGuards instead Other two type ignores here are pending update of typeshed in mypy: python/typeshed#5473 * Code review feedback Co-authored-by: davfsa <davfsa@gmail.com> Co-authored-by: FasterSpeeding <lucina@lmbyrne.dev> * Use mypy attrs plugin more effectively * Fix merge conflicts * Remove redundancy in config * Re-order type overloads and abstract method decorators Now, this PR follows the convention found elsewhere in the library. (In hikari/api/rest.py) * Actually run converters, as well as have a docstring for ssl * davfsa patch to cache, with a couple changes * Don't run converters on setattr * Fix Cache3DMappingView (why isn't mypy warning me about this!!) * Remove transparent mapping * Remove redundant else clause * Reformat Co-authored-by: davfsa <davfsa@gmail.com> Co-authored-by: FasterSpeeding <lucina@lmbyrne.dev>