Stack Overflow Asked on November 20, 2021
Goal: restrict function argument to only accept the numpy
or tensorflow
module.
Some ideas I have, use Literal
types (PEP586):
Literal["np", "tf"]
Then use globals()
to find the symbol. This would work reasonably well, though obviously skips the module type check.
Is there some way I can accept either the str representation or the module itself? – Passing in the module itself gave this error:
Expected type
Literal["np", "tf"]
, gotUnion[__init__.py, __init__.py]
instead
But I don’t know how to create Union[__init__.py, __init__.py]
, e.g., this doesn’t work:
Union[Union[__init__.py, __init__.py], Literal["np", "tf"]]
Do you need to use type checking? You could assert like so
assert module == np or module == tf, f"{module.__name__} not numpy or tensorflow"
Answered by Evan on November 20, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP