Skip to content
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

Allow stubtest to raise errors on abstract state mismatch #13323

Merged
merged 6 commits into from
Aug 10, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Aug 4, 2022

Closes #13322

@AlexWaygood
Copy link
Member

For reference, here is my patch that I worked on ages ago, but never submitted a PR for: AlexWaygood@c031ffd

It has a few additional checks to yours, which you might be interested in possibly adding :)

@@ -825,6 +825,13 @@ def verify_funcitem(
if not callable(runtime):
return

if isinstance(stub, nodes.FuncDef):
stub_abstract = stub.abstract_status == nodes.IS_ABSTRACT
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can safely skip nodes.IMPLICITLY_ABSTRACT here. Because it can only happen for a protocol.

@sobolevn
Copy link
Member Author

sobolevn commented Aug 4, 2022

@AlexWaygood thanks! I think that

if inspect.isabstract(runtime) and not stub.is_abstract:

part is not really required. Because mypy sets is_abstract if any methods inside a class are abstract. The same for inspect.isabstract

Short example:

>>> import abc, inspect
>>> class A(abc.ABC): ...
... 
>>> inspect.isabstract(A)
False

So, checking that stubs have >= abstact methods is just fine. Abstract classes will be checked by this as well. Moreover, stubtest also warns about metaclass=ABCMeta for types with abstract members.

@AlexWaygood
Copy link
Member

Are abstract properties accounted for?

@sobolevn
Copy link
Member Author

sobolevn commented Aug 4, 2022

Great question! New tests are added.

mypy/stubtest.py Outdated Show resolved Hide resolved
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! :)

@AlexWaygood
Copy link
Member

There's no diff when running typeshed's stubtest_stdlib.py with this patch checked out; this doesn't look like it would cause any disruption for typeshed :)

@sobolevn
Copy link
Member Author

sobolevn commented Aug 9, 2022

Awesome! I am going to merge it then 🎉
(with a day delay)

mypy/stubtest.py Outdated Show resolved Hide resolved
@sobolevn sobolevn merged commit e77ee3b into python:master Aug 10, 2022
sobolevn pushed a commit that referenced this pull request Mar 24, 2023
…n the stub is an overloaded method (#14955)

#13323 means that stubtest will currently emit an error if the runtime
is decorated with `@abstractmethod` but the stub is not, but _only_ if
the stub is not an overloaded function, as overloaded functions have a
different internal representation in mypy. This PR fixes that
discrepancy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stubtest produce no error when @abstractmethod is omitted from stubs
3 participants