Skip to content

Allow access to class attributes/methods if __new__/__init__ return Never/NoReturn - #22279

Open
zedzhen wants to merge 2 commits into
python:masterfrom
zedzhen:fix-16981
Open

zedzhen wants to merge 2 commits into
python:masterfrom
zedzhen:fix-16981

Conversation

@zedzhen

@zedzhen zedzhen commented Oct 22, 2026 •

Copy link
Copy Markdown

Fixes #16981

If __new__/__init__ return Never/NoReturn, after this fix, you can get attribute/method from class.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

strawberry (https://github.com/strawberry-graphql/strawberry)
+ strawberry/types/field.py:348: error: Unused "type: ignore" comment  [unused-ignore]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/builders/_epub_base.py:34:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
+ sphinx/testing/util.py:80:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

Comment thread mypy/types.py
get_proper_type(self.ret_type), UninhabitedType
return self.fallback.type.is_metaclass() and (
self.instance_type is not None
or not isinstance(get_proper_type(self.ret_type), UninhabitedType)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is the or not isinstance(get_proper_type(self.ret_type), UninhabitedType) still needed (after #21441)? can we remove it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

if remove only or not isinstance(get_proper_type(self.ret_type), UninhabitedType) in pytest-fast failed 11 tests:

FAILED mypy/test/testcheck.py::TypeCheckSuite::check-dataclasses.test::testDataclassDefaultFactoryTypedDict - data: [PROJECT_ROOT]\test-data\unit\check-dataclasses.test:2893:
FAILED mypy/test/testcheck.py::TypeCheckSuite::check-type-object-type-inference.test::testInferTupleType - data: [PROJECT_ROOT]\test-data\unit\check-type-object-type-inference.test:1:
FAILED mypy/test/testdeps.py::GetDependenciesSuite::deps.test::testTypedDict - data: [PROJECT_ROOT]\test-data\unit\deps.test:659:
FAILED mypy/test/testdeps.py::GetDependenciesSuite::deps.test::testTypedDict2 - data: [PROJECT_ROOT]\test-data\unit\deps.test:673:
FAILED mypy/test/testdeps.py::GetDependenciesSuite::deps.test::testTypedDict3 - data: [PROJECT_ROOT]\test-data\unit\deps.test:691:
FAILED mypy/test/testcheck.py::TypeCheckSuite::check-typeddict.test::testTypedDictWithIsInstanceAndIsSubclass - data: [PROJECT_ROOT]\test-data\unit\check-typeddict.test:961:
FAILED mypy/test/testcheck.py::TypeCheckSuite::check-typeddict.test::testGenericTypedDictCreation - data: [PROJECT_ROOT]\test-data\unit\check-typeddict.test:3302:
FAILED mypy/test/testcheck.py::TypeCheckSuite::check-typeddict.test::testTypedDictAttributeOnClassObject - data: [PROJECT_ROOT]\test-data\unit\check-typeddict.test:3414:
FAILED mypy/test/testcheck.py::TypeCheckSuite::check-typeddict.test::testGenericTypedDictCallSyntax - data: [PROJECT_ROOT]\test-data\unit\check-typeddict.test:3465:
FAILED mypy/test/testtypes.py::JoinSuite::test_simple_type_objects - assert False
FAILED mypy/test/testsubtypes.py::SubtypingSuite::test_type_callable_subtyping - AssertionError: def (A) -> B subtype of def (A) -> B

if save only return self.fallback.type.is_metaclass(), tests pass, but get_instance_type may start returning UninhabitedType, and then type_object will fail on assert isinstance(instance_type, Instance) (on types created by plugins that do not set instance_type, but ret_type is UninhabitedType).

@zedzhen
zedzhen requested a review from hauntsaninja October 23, 2026 10:38
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.

Using Never as return type for __new__ results in classmethods not being recognised. Inferred type for class is Callable[[], NoReturn].

2 participants