-
-
Notifications
You must be signed in to change notification settings - Fork 3k
stubtest: error if a dunder method is missing from a stub #12203
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
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
62b5875
stubtest: error if a dunder method is missing from a stub
AlexWaygood 8324e2f
flake8
AlexWaygood ce0cc14
py36 fixes (ew)
AlexWaygood 8e49d6b
Merge remote-tracking branch 'origin/master' into stubtest
AlexWaygood 4425a3b
Make tests actually test what I want them to test
AlexWaygood 6195e20
Merge branch 'python:master' into stubtest
AlexWaygood ba25ed0
Improve py36 workaround, add comment re `__match_args__`
AlexWaygood 52097ba
Merge branch 'stubtest' of https://github.com/AlexWaygood/mypy into s…
AlexWaygood e7080a9
Merge branch 'python:master' into stubtest
AlexWaygood 749b2c3
Remove 5 methods from the ignorelist
AlexWaygood cfb4c3a
Get rid of 3rd arg in `getattr` call
AlexWaygood a71b1b3
Move logic from `verify_none` into `verify_typeinfo`
AlexWaygood 1720eaa
Death to the `is_dunder_slot_wrapper` helper function
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
from mypy import nodes | ||
from mypy.config_parser import parse_config_file | ||
from mypy.options import Options | ||
from mypy.util import FancyFormatter, bytes_to_human_readable_repr, is_dunder, SPECIAL_DUNDERS | ||
from mypy.util import FancyFormatter, bytes_to_human_readable_repr, is_dunder | ||
|
||
|
||
class Missing: | ||
|
@@ -243,6 +243,58 @@ def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool: | |
) | ||
|
||
|
||
IGNORED_DUNDERS = frozenset({ | ||
# Very special attributes | ||
"__weakref__", | ||
"__slots__", | ||
"__dict__", | ||
"__text_signature__", | ||
"__match_args__", | ||
# Pickle methods | ||
"__setstate__", | ||
"__getstate__", | ||
"__getnewargs__", | ||
"__getinitargs__", | ||
"__reduce_ex__", | ||
"__reduce__", | ||
# typing implementation details | ||
"__parameters__", | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"__origin__", | ||
"__args__", | ||
"__orig_bases__", | ||
"__mro_entries__", | ||
"__forward_is_class__", | ||
"__forward_module__", | ||
"__final__", | ||
# isinstance/issubclass hooks that type-checkers don't usually care about | ||
"__instancecheck__", | ||
"__subclasshook__", | ||
"__subclasscheck__", | ||
# Dataclasses implementation details | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"__dataclass_fields__", | ||
"__dataclass_params__", | ||
# ctypes weirdness | ||
"__ctype_be__", | ||
"__ctype_le__", | ||
"__ctypes_from_outparam__", | ||
# Two float methods only used internally for CPython test suite, not for public use | ||
"__set_format__", | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"__getformat__", | ||
# These two are basically useless for type checkers | ||
"__hash__", | ||
"__getattr__", | ||
# For some reason, mypy doesn't infer classes with metaclass=ABCMeta inherit this attribute | ||
"__abstractmethods__", | ||
"__doc__", # Can only ever be str | None, who cares? | ||
"__del__", # Only ever called when an object is being deleted, who cares? | ||
"__new_member__", # If an enum defines __new__, the method is renamed as __new_member__ | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a fairly arbitrary ignorelist, based on the hits that this patch initially came up with, combined with my judgement as to which were worth fixing and which weren't. I'm happy to add or remove items! |
||
|
||
|
||
def is_private(name: str) -> bool: | ||
return name.startswith("_") and not is_dunder(name) | ||
|
||
|
||
@verify.register(nodes.TypeInfo) | ||
def verify_typeinfo( | ||
stub: nodes.TypeInfo, runtime: MaybeMissing[Type[Any]], object_path: List[str] | ||
|
@@ -274,11 +326,9 @@ class SubClass(runtime): # type: ignore | |
|
||
# Check everything already defined in the stub | ||
to_check = set(stub.names) | ||
# There's a reasonable case to be made that we should always check all dunders, but it's | ||
# currently quite noisy. We could turn this into a denylist instead of an allowlist. | ||
to_check.update( | ||
# cast to workaround mypyc complaints | ||
m for m in cast(Any, vars)(runtime) if not m.startswith("_") or m in SPECIAL_DUNDERS | ||
m for m in cast(Any, vars)(runtime) if not is_private(m) and m not in IGNORED_DUNDERS | ||
) | ||
|
||
for entry in sorted(to_check): | ||
|
@@ -713,7 +763,15 @@ def verify_funcitem( | |
def verify_none( | ||
stub: Missing, runtime: MaybeMissing[Any], object_path: List[str] | ||
) -> Iterator[Error]: | ||
yield Error(object_path, "is not present in stub", stub, runtime) | ||
# Do not error for an object missing from the stub | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# If the runtime object is a types.WrapperDescriptorType object | ||
# and has a non-special dunder name. | ||
# The vast majority of these are false positives. | ||
if not ( | ||
isinstance(runtime, type(object.__init__)) | ||
and is_dunder(getattr(runtime, "__name__", ""), exclude_special=True) | ||
): | ||
yield Error(object_path, "is not present in stub", stub, runtime) | ||
|
||
|
||
@verify.register(nodes.Var) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.