stubtest: analyze `metaclass` of types, refs #13327
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:
- 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 addingand 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? ExitStack: https://github.com/python/typeshed/issues/7961 This is a real issue with these classes. We can at least addABCMetametaclass to them
CC @srittau and @AlexWaygood Closes #13327
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.
I agree with @srittau — I'm fine with adding private classes to stubs if they're used by public classes.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
I want to merge this, any feedback, @hauntsaninja or @JelleZijlstra? 🙂
Thanks everyone!