-
Notifications
You must be signed in to change notification settings - Fork 93
Add support for type annotations, typing.TypedDict and python dataclasses #167
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
Open
jvllmr
wants to merge
6
commits into
klen:develop
Choose a base branch
from
jvllmr:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b13c7a6
add support for type annotated classes and typing.TypedDict
jvllmr 943c04b
add information about type annotations to readme and changelog
jvllmr dec7410
add support for python dataclasses
jvllmr c5aac62
change metadata
jvllmr 9294383
upgrade development environemt
jvllmr f8ad19b
fix tests; use sqlalchemy random implementation
jvllmr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| 2022-07-10 | ||
| * Add support for type annotations and typing.TypedDict | ||
|
|
||
| 2022-03-23 | ||
| * Django: Support for UUIDField | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from ..main import Mixer as BaseMixer, TypeMixer as BaseTypeMixer, GenFactory as BaseGenFactory | ||
| import typing | ||
| import mixer.mix_types as t | ||
| import sys | ||
| import random | ||
|
|
||
|
|
||
| class GenFactory(BaseGenFactory): | ||
|
|
||
| @classmethod | ||
| def cls_to_simple(cls, fcls): | ||
| if hasattr(fcls, "__origin__"): | ||
| if fcls.__origin__ == typing.Union: | ||
| return random.choice(fcls.__args__) | ||
|
|
||
| return super().cls_to_simple(fcls) | ||
|
|
||
|
|
||
| class TypeMixer(BaseTypeMixer): | ||
| factory = GenFactory | ||
|
|
||
|
|
||
| def populate_target(self, values): | ||
| if issubclass(self.__scheme, dict): | ||
| return self.__scheme(**{k: v for k,v in values}) | ||
|
|
||
| return super().populate_target(values) | ||
|
|
||
| def __load_fields(self): | ||
| annotations = getattr(self.__scheme, "__annotations__", None) | ||
| if annotations is None: | ||
| raise ValueError(f"Class {self.__scheme} has no type annotations") | ||
|
|
||
| # https://docs.python.org/3/howto/annotations.html#manually-un-stringizing-stringized-annotations | ||
| for fname, prop in annotations.items(): | ||
| if fname.startswith("_"): | ||
| continue | ||
|
|
||
|
|
||
| try: | ||
| obj_globals = sys.modules[self.__scheme.__module__].__dict__ | ||
| except (AttributeError, KeyError): | ||
| obj_globals = None | ||
|
|
||
| if isinstance(prop, typing.ForwardRef): | ||
| prop = prop.__forward_arg__ | ||
|
|
||
| prop_type = eval(prop, obj_globals, dict(vars(self.__scheme))) | ||
|
|
||
| yield fname, t.Field( | ||
| prop_type, | ||
| fname, | ||
| ) | ||
|
|
||
|
|
||
| class Mixer(BaseMixer): | ||
| type_mixer_cls = TypeMixer | ||
|
|
||
|
|
||
| mixer = Mixer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ pony >= 0.7 | |
| psycopg2-binary >= 2.8.4 | ||
|
|
||
| pytest | ||
| typing_extensions | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| from __future__ import annotations | ||
| import typing as t | ||
| import typing_extensions as te | ||
| from mixer.backend.annotated import Mixer | ||
|
|
||
|
|
||
| class Test: | ||
| one: int | ||
| two: int | ||
| name: str | ||
| title: str | ||
| body: str | ||
| price: t.Optional[int] | ||
| something: t.Union[int, str] | ||
| choices: list | ||
| parts: set | ||
| scheme: dict | ||
|
|
||
|
|
||
| def test_factory(): | ||
| from mixer.backend.annotated import GenFactory | ||
|
|
||
| g = GenFactory() | ||
| test = g.get_fabric(t.Optional[int]) | ||
| assert isinstance(test(), (int, type(None))) | ||
|
|
||
| test = g.get_fabric(t.Union[str, int]) | ||
| assert isinstance(test(), (str, int)) | ||
|
|
||
|
|
||
| def test_annotated(): | ||
| mixer = Mixer() | ||
| test = mixer.blend(Test) | ||
| assert isinstance(test.price, (int, type(None))) | ||
| assert isinstance(test.something, (int, str)) | ||
|
|
||
|
|
||
| def test_typed_dict(): | ||
| class TestDict(te.TypedDict): | ||
| one: int | ||
| two: int | ||
| name: str | ||
|
|
||
| mixer = Mixer() | ||
| test = mixer.blend(TestDict) | ||
|
|
||
| assert isinstance(test, dict) | ||
| assert "one" in test and "two" in test and "name" in test | ||
| assert isinstance(test["one"], int) and isinstance(test["two"], int) and isinstance(test["name"], str) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was there an empty string value?