Resolve original type object from a type string literal #1382
Closed
abelcheung
started this conversation in
General
Replies: 1 comment 1 reply
-
You need knowledge of namespaces (globals/locals) where it should be evaluated. Usually for type annotations as string that come from class attribute/function signature you can try to fetch related namespace of that class/module. But if you have string with no context where it was used to define type you don't have enough information. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Given a type as string, say
"int"
, I can get the original class easily since it's a builtin type and resolvable witheval("int")
. But is there any way to get other types or generic aliases working, likeForwardRef('Literal')._evaluate()
but can automatically determine where otherwise unresolvable names would come from?I think it could be sort of attempted by importing common modules first (such as
collections.abc
,typing
etc), or by exhaustively creating mapping of unresolvable common names and their corresponding modules. But I bet somebody might have created a better prior art, so it could be better to ask here first.Beta Was this translation helpful? Give feedback.
All reactions