-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Support lists #37
Comments
You can just write
|
@JelleZijlstra is that backwards compatible, even to Python 3.6?
|
Works in both python3.6 and python3.7 def is_list_type(tp) -> bool:
"""
Test if the type is a generic list type, including subclasses excluding
non-generic classes.
Examples::
is_list_type(int) == False
is_list_type(list) == False
is_list_type(List) == True
is_list_type(List[str, int]) == True
class MyClass(List[str]):
...
is_list_type(MyClass) == True
"""
return is_generic_type(tp) and issubclass(get_origin(tp) or tp, List) And from tests: params: List[Tuple[str, type, bool]] = \
[
("int", int, False),
("list", list, False),
("List", List, True),
("List[int]", List[int], True),
("ListChild", ListChild, True),
("List[ListChild]", List[ListChild], True),
("List[CustomJsonDataclass]", List[CustomJsonDataclass], True),
("CustomJsonDataclass", CustomJsonDataclass, False),
("GenericListChild", GenericListChild, True),
("GenericListChild[int, bool]", GenericListChild[int, bool], True),
]
for name, tp, expected in params:
with self.subTest(name, expected=expected):
self.assertEqual(expected, is_list_type(tp)) |
See also |
Fwiw, I arrived here looking for something like The above solutions did not work for me given that I dont know the incoming annotation, which are not always
I ended up with (although i may be misinformed and this fails in ways I dont yet understand): def is_subclass(typ, superclass):
if not isinstance(typ, type):
return False
return issubclass(typ, superclass)
def is_sequence_type(typ):
return is_subclass(get_origin(typ) or typ, SUPPORTED_SEQUENCE_TYPES)
SUPPORTED_SEQUENCE_TYPES = (typing.List, typing.Tuple, typing.Set) In my humble, uneducated opinion it'd be ideal if this library were able to include runtime equivalents for these sorts of things that have corresponding values-apis in stdlib, even if (as the comments towards the top imply) it's "easy" to compose them from other apis already exposed from this library. |
What is sequence in your definition? Is it any iterable, or collection of finite size? One thing you can do is to check if the class matches the protocol via magic Merida methods — |
For my purposes, i'm talking about equivalency to Sequence, although now that i'm looking at it, i was But I wouldn't be surprised if there were some weird false positives in the context of the type system, given that any generic type is going implement mostly posted to suggest that i think there's value in a library like this one implementing these "easily" derivable runtime checks for common use, which the earlier comments seemed to be advocating against. |
Could you support lists in your api?
In example, have such functions:
The text was updated successfully, but these errors were encountered: