Issue
I am using Typeguard in a couple if projects for type checking at run time in Python. It works pretty well.
I have encountered a situation where the type of a function parameter is a typing.Union
made up of a few dynamically collected data types. E.g.
def find_datatypes():
# some stuff ...
return (str, int) # dynamically generated list / tuple
datatypes = find_datatypes()
Now I want to generate a typing.Union
from datatypes
for eventual use in a function. I expected unpacking syntax to work:
my_union = typing.Union[*datatypes]
@typeguard.typechecked
def some_function(param: my_union):
pass
However, it did not:
my_union = typing.Union[*datatypes]
^
SyntaxError: invalid syntax
How would I achieve what I want?
Solution
You can kind of do it:
my_union = typing.Union[datatypes]
At runtime, thing[x, y]
is already equivalent to thing[(x, y)]
.
That said, there are limitations to keep in mind. Particularly, when using string annotations (which will become the default at some point - currently scheduled for Python 3.11), my_union
will have to be available in some_function
's global namespace for typeguard
or anything else to be able to resolve the annotation at runtime. That restricts a lot of closure use cases, and a lot of attempts to add annotations dynamically.
Also, as you might expect, mypy will not consider any of this valid.
Answered By - user2357112 supports Monica
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.