Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Type-hinting code to retrieve values of a typing.Literal behind a TypeDefAlias

$
0
0

Consider the following code, written for Python 3.12 (leveraging PEP-695):

import typingtype Animal = typing.Literal["dog", "cat"]def enum_values(t: typing.TypeAliasType) -> typing.Sequence[str]:    target = typing.cast(typing.Literal, t.__value__)    return typing.get_args(target)print(repr(enum_values(Animal)))

When invoked, it properly emits as output:

('dog', 'cat')

but it fails type checking with pyright:

testme.py:9:24 - error: Argument of type "Animal" cannot be assigned to parameter "t" of type "TypeAliasType" in function "enum_values"  Type "Animal" cannot be assigned to type "TypeAliasType"    "type[type]" is incompatible with "type[TypeAliasType]" (reportGeneralTypeIssues)1 error, 0 warnings, 0 informations

How would a correct type signature for that function be written?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>