mypy icon indicating copy to clipboard operation
mypy copied to clipboard

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

Open sobolevn opened this issue 3 years ago • 3 comments

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: https://github.com/python/typeshed/issues/7961 This is a real issue with these classes. We can at least add ABCMeta metaclass to them

CC @srittau and @AlexWaygood Closes #13327

sobolevn avatar Aug 05 '22 10:08 sobolevn

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.

srittau avatar Aug 05 '22 10:08 srittau

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

AlexWaygood avatar Aug 05 '22 10:08 AlexWaygood

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

github-actions[bot] avatar Aug 05 '22 10:08 github-actions[bot]

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

sobolevn avatar Aug 22 '22 20:08 sobolevn

Thanks everyone!

sobolevn avatar Aug 25 '22 05:08 sobolevn