Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import typing
- def construct_query_class(cls):
- annotations = {key: typing.Optional[value] for key, value in cls.__annotations__.items()}
- return type(cls.__name__ + 'Query', (), {'__annotations__': annotations})
- class SomeResource:
- id: int
- name: str
- SomeResourceQuery = construct_query_class(SomeResource)
- if __name__ == "__main__":
- test0: SomeResource = SomeResource()
- test0.id = 1
- test0.name = "stian"
- test: SomeResourceQuery = SomeResourceQuery()
- test.id = 1 # Pycharm isn't helping with autocompletion here
- # Also, mypy has this to say about this file:
- # testing.py:21: error: Variable "testing.SomeResourceQuery" is not valid as a type
- # testing.py:21: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- # testing.py:22: error: SomeResourceQuery? has no attribute "id"
- # Found 2 errors in 1 file (checked 1 source file)
Add Comment
Please, Sign In to add comment