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

update allowlist comment for SupportsAbs and SupportsRound #13380

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Jan 9, 2025

The answer to the question is that mypy infers it just fine, but stubtest doesn't check for attributes inherited from the metaclass because that's almost never what we want. In this case, the runtime implementation doesn't get __type_params__ from builtins.type either. It's actually defined for the class due to the use of PEP-695 syntax.

We can see the difference between inheriting from type and having it on the class:

class Foo(SupportsAbs):
    def __abs__(self):
        return 1

class Bar(SupportsIndex):
    def __index__(self):
        return 1

Given these classes:

>>> Foo.__type_params__
()
>>> Foo().__type_params__
(T,)
>>> Bar.__type_params__
()
>>> Bar().__type_params__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Bar' object has no attribute '__type_params__'

or alternatively:

>>> getattr_static(SupportsAbs, '__type_params__')
(T,)
>>> getattr_static(SupportsIndex, '__type_params__')
<attribute '__type_params__' of 'type' objects>

This might go away eventually when typeshed adopts PEP-695 syntax for it's type variables? I'm not sure.

The answer to the question is that mypy infers it just fine, but
stubtest doesn't check for attributes inherited from the metaclass
because that's almost never what we want. In this case,
the runtime implementation doesn't get __type_params__ from
builtins.type either. It's actually defined for the class due to
the use of PEP-695 syntax.
Copy link
Contributor

github-actions bot commented Jan 9, 2025

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

@hauntsaninja hauntsaninja merged commit e55c13a into python:main Jan 9, 2025
64 checks passed
@hauntsaninja
Copy link
Collaborator

Thank you!

@tungol tungol deleted the typing branch January 9, 2025 22:10
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.

2 participants