Closed Ashark closed 9 months ago
You should be able to take advantage of the standard comma separated syntax with the unpacking *
operator. Other similar variants of this passing into Union would also work.
def func(way) -> list[str, *WithSubclasses(Dev)]:
This way the list is defined not as containing any element of any type (from union) in any order.
In other words,
def func(way) -> list[str, int]:
and
def func(way) -> list[str | int]:
are not the same thing.
The list[str, int]
means only the following list may be valid: ["apple", 300, "orange", 400]
(i.e., order matters).
But with list[str | int]
these both may be valid: ["apple", 300, "orange", 400]
, ["apple", "orange", 400, 500, "banana"]
. In the last case I do not have to even keep the "pairs" of elements.
So in this case, type_enforced deviates from the typing package expectations. Essentially unions and list separated types are equivalent (even inside of lists).
Essentially: list[str,int]
== Union(list[str], list[int])
== list[Union(str, int)]
== list[str | int]
This is defined in supported types and through the docs.
If you prefer to keep the syntax:
def func(way) -> list[Union(str, *WithSubclasses(Dev))]:
I will go ahead and close this issue.
I have the following code:
For my function
func
I previously used just such annotation:-> list
. I now wanted to be a bit more specific, so I wanted to specify thatfunc
may return a list that may containstr
or aDev
or any subclass ofDev
. I then tried to use such annotation:-> list[str | Dev]
. And this code works normally by itself, but type_enforcer does not accept the subclasses ofDev
.What is the usage of
WithSubclasses
for such case? I have found that I can use this:It would be good to support a more nice syntax.