Skip to content

Conversation

@byteforge38
Copy link

Fixes #20585

When a callable has type[Any] as its return type (e.g., from dict.get(key, type[Any])), calling it would crash with AssertionError: isinstance(ret, Instance).

The issue was in is_type_obj() which returned True for type[Any] because type is a metaclass and AnyType isn't UninhabitedType. However, type_object() then failed because it expects an Instance, not AnyType.

The fix excludes AnyType from is_type_obj() since we can't determine a concrete type object when the return type is Any.


Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=4217675

@byteforge38 byteforge38 force-pushed the fix-type-any-assertion-error branch from 6d12836 to c0fcad8 Compare January 15, 2026 03:55
# Regression test for https://github.com/python/mypy/issues/20585
from typing import Any
op: type[Any]
reveal_type({0: object}.get(0, op)()) # N: Revealed type is "builtins.object"
Copy link
Member

Choose a reason for hiding this comment

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

I would have expected Any, not object. Is there a way to reproduce the crash without relying on .get()? And did you verify this line crashed without the fix? Our fixture may be simpler than the actual typeshed stubs for dict.get.

Copy link
Author

Choose a reason for hiding this comment

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

I added two unit tests to mypy/test/testtypes.py, and yes, I was able to reproduce that crash

@github-actions

This comment has been minimized.

@byteforge38 byteforge38 force-pushed the fix-type-any-assertion-error branch from c0fcad8 to 4224b93 Compare January 15, 2026 04:26
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

I don't like the object return but it sounds like that will need some special casing :/

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.

Assertion error: isinstance(ret, Instance)

3 participants