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

stubtest: analyze metaclass of types, refs #13327 #13331

Merged
merged 7 commits into from
Aug 25, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Aug 5, 2022

Current typeshed produces:

error: ast.Bytes metaclass missmatch
Stub: at line 16
Missing metaclass
Runtime: at line 513 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/ast.py
Exiting metaclass: <class 'ast._ABC'>

error: ast.Ellipsis metaclass missmatch
Stub: at line 22
Missing metaclass
Runtime: at line 520 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/ast.py
Exiting metaclass: <class 'ast._ABC'>

error: ast.NameConstant metaclass missmatch
Stub: at line 21
Missing metaclass
Runtime: at line 517 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/ast.py
Exiting metaclass: <class 'ast._ABC'>

error: ast.Num metaclass missmatch
Stub: at line 8
Missing metaclass
Runtime: at line 505 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/ast.py
Exiting metaclass: <class 'ast._ABC'>

error: ast.Str metaclass missmatch
Stub: at line 11
Missing metaclass
Runtime: at line 509 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/ast.py
Exiting metaclass: <class 'ast._ABC'>

error: contextlib.AsyncExitStack metaclass missmatch
Stub: at line 151
Missing metaclass
Runtime: at line 537 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/contextlib.py
Exiting metaclass: <class 'abc.ABCMeta'>

error: contextlib.ExitStack metaclass missmatch
Stub: at line 136
Missing metaclass
Runtime: at line 468 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/contextlib.py
Exiting metaclass: <class 'abc.ABCMeta'>

error: dataclasses.InitVar metaclass missmatch
Stub: at line 220
Missing metaclass
Runtime: at line 206 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/dataclasses.py
Exiting metaclass: <class 'dataclasses._InitVarMeta'>

error: string.Template metaclass missmatch
Stub: at line 35
Missing metaclass
Runtime: at line 80 in file /Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/string.py
Exiting metaclass: <class 'string._TemplateMetaclass'>

There are two different problems:

  1. There are protected metaclasses that do not exist, like string._TemplateMetaclass, because we mostly don't add protected names to stubs. This can be solved by adding and not is_probably_private(stub.metaclass_type.type.fullname) check (level: primitive). I just wanted to show the full picture: maybe we do want to add them to stubs?
  2. ExitStack: Wrong return type from ExitStack.enter_context when given a child of ExitStack typeshed#7961 This is a real issue with these classes. We can at least add ABCMeta metaclass to them

CC @srittau and @AlexWaygood
Closes #13327

@srittau
Copy link
Contributor

srittau commented Aug 5, 2022

While we usually don't add private elements to stubs, I think we should make an exception for base and metaclasses. Having a correct mro seems more important to me than omitting those classes.

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

I agree with @srittau — I'm fine with adding private classes to stubs if they're used by public classes.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@sobolevn
Copy link
Member Author

I want to merge this, any feedback, @hauntsaninja or @JelleZijlstra? 🙂

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@sobolevn sobolevn merged commit 55d757e into python:master Aug 25, 2022
@sobolevn
Copy link
Member Author

Thanks everyone!

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: metaclass is not taken into an account
4 participants