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

[mypyc] Using UnboundedType to access class object of a type annotation. #18874

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

advait-dixit
Copy link
Contributor

Fixes mypyc/mypyc#1087.

This fix handles cases where type annotation is nested inside an imported module (like an inner class) or imported from a different module.

@advait-dixit advait-dixit changed the title [mypyc] Using unbounded_type to access class object of a type annotation. [mypyc] Using UnboundedType to access class object of a type annotation. Apr 5, 2025
@advait-dixit
Copy link
Contributor Author

@JukkaL @aatle : This PR is ready for review.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Not a full review yet, but left a few comments.

@@ -78,17 +78,22 @@ assert hasattr(c, 'x')

[case testTypedDictWithFields]
import collections
import json
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if as is used in the import, such as import json as _json? There could even be a case like this:

import a as b
import b as a
...
# b.C in an annotation refers to fullname a.C!

Similarly, what if we have from pkg import submod and then submod.C is used in an annotation?

It's okay to not support all possible cases yet -- we can create follow-up issues -- but it would be nice if we can avoid having references to incorrect types.

@@ -802,15 +802,41 @@ def get_func_target(builder: IRBuilder, fdef: FuncDef) -> AssignmentTarget:
return builder.add_local_reg(fdef, object_rprimitive)


def load_type(builder: IRBuilder, typ: TypeInfo, line: int) -> Value:
def load_type(builder: IRBuilder, typ: TypeInfo, unbounded_type: Type | None, line: int) -> Value:
# typ.fullname contains the module where the class object was defined. However, it is possible that the class
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: some of the comments seem to be longer than 99 characters, which is our line length limit. Can you wrap any long lines?

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.

Compiled class definition involving TypedDict or dataclass causes KeyError for certain annotations
2 participants