From c9732c192f46a7a02872852f0a13b5745fe443e9 Mon Sep 17 00:00:00 2001 From: Artur Barseghyan Date: Thu, 21 Dec 2023 21:14:10 +0100 Subject: [PATCH] Fix/fixes (#57) * Fixes * Mention examples --- README.rst | 2 ++ examples/README.rst | 11 +++++++++ examples/customization/README.rst | 8 +++++++ examples/dataclasses/README.rst | 4 ++++ examples/django/README.rst | 3 +++ examples/hypothesis/README.rst | 3 +++ examples/lazyfuzzy/README.rst | 5 ++++ examples/pydantic/README.rst | 3 +++ examples/sqlalchemy/README.rst | 3 +++ examples/sqlalchemy/article/factories.py | 30 ++++++++++++------------ examples/tortoise/README.rst | 4 ++++ fake.py | 4 ++-- 12 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 examples/README.rst create mode 100644 examples/customization/README.rst create mode 100644 examples/dataclasses/README.rst create mode 100644 examples/django/README.rst create mode 100644 examples/hypothesis/README.rst create mode 100644 examples/lazyfuzzy/README.rst create mode 100644 examples/pydantic/README.rst create mode 100644 examples/sqlalchemy/README.rst create mode 100644 examples/tortoise/README.rst diff --git a/README.rst b/README.rst index 5e9f246..829d3e5 100644 --- a/README.rst +++ b/README.rst @@ -25,6 +25,7 @@ fake.py .. _Creating DOCX: https://fakepy.readthedocs.io/en/latest/creating_docx.html .. _Creating images: https://fakepy.readthedocs.io/en/latest/creating_images.html .. _Contributor guidelines: https://fakepy.readthedocs.io/en/latest/contributor_guidelines.html +.. _Examples: https://github.com/barseghyanartur/fake.py/tree/main/examples Minimalistic, standalone alternative fake data generator with no dependencies. @@ -106,6 +107,7 @@ Documentation - For tips on ``DOCX`` creation see `Creating DOCX`_. - For tips on images creation see `Creating images`_. - For guidelines on contributing check the `Contributor guidelines`_. +- For various implementation examples, see the `Examples`_. Usage ===== diff --git a/examples/README.rst b/examples/README.rst new file mode 100644 index 0000000..7617e29 --- /dev/null +++ b/examples/README.rst @@ -0,0 +1,11 @@ +fake.py examples +================ + +- `Customization `_. +- `Dataclasses `_. +- `Django `_. +- `Hypothesis `_. +- `Lazy/Fuzzy `_. +- `Pydantic `_. +- `SQLAlchemy `_. +- `TortoiseORM `_. diff --git a/examples/customization/README.rst b/examples/customization/README.rst new file mode 100644 index 0000000..d164ed7 --- /dev/null +++ b/examples/customization/README.rst @@ -0,0 +1,8 @@ +Customization +============= +Various customization examples: + +- Address provider implementation. +- Band provider implementation. +- Replace built-in fake data corpus/vocabulary with a custom one. +- Override default data. diff --git a/examples/dataclasses/README.rst b/examples/dataclasses/README.rst new file mode 100644 index 0000000..7927424 --- /dev/null +++ b/examples/dataclasses/README.rst @@ -0,0 +1,4 @@ +Dataclasses +=========== +Example factories for models based on +`dataclasses `_. diff --git a/examples/django/README.rst b/examples/django/README.rst new file mode 100644 index 0000000..46e065b --- /dev/null +++ b/examples/django/README.rst @@ -0,0 +1,3 @@ +Django +====== +Example factories for `Django `_ models. diff --git a/examples/hypothesis/README.rst b/examples/hypothesis/README.rst new file mode 100644 index 0000000..74432e8 --- /dev/null +++ b/examples/hypothesis/README.rst @@ -0,0 +1,3 @@ +Hypothesis +========== +Additional tests using `hypothesis `_. diff --git a/examples/lazyfuzzy/README.rst b/examples/lazyfuzzy/README.rst new file mode 100644 index 0000000..18ae424 --- /dev/null +++ b/examples/lazyfuzzy/README.rst @@ -0,0 +1,5 @@ +Lazy/Fuzzy +========== +- A sample ``FuzzyChoice`` alternative implementation based on + ``LazyFunction`` and ``functools.partial``. +- Various usage examples of ``LazyFunction`` and ``LazyAttribute``. diff --git a/examples/pydantic/README.rst b/examples/pydantic/README.rst new file mode 100644 index 0000000..76b3869 --- /dev/null +++ b/examples/pydantic/README.rst @@ -0,0 +1,3 @@ +Pydantic +======== +Example factories for models based on `Pydantic `_. diff --git a/examples/sqlalchemy/README.rst b/examples/sqlalchemy/README.rst new file mode 100644 index 0000000..0e416ab --- /dev/null +++ b/examples/sqlalchemy/README.rst @@ -0,0 +1,3 @@ +SQLAlchemy +========== +Example factories for models based on `SQLAlchemy `_. diff --git a/examples/sqlalchemy/article/factories.py b/examples/sqlalchemy/article/factories.py index 92c5d0c..1e94336 100644 --- a/examples/sqlalchemy/article/factories.py +++ b/examples/sqlalchemy/article/factories.py @@ -70,15 +70,15 @@ def add_to_group(user: User, name: str) -> None: class UserFactory(SQLAlchemyModelFactory): """User factory.""" - username = FACTORY.username() # mypy: ignore - first_name = FACTORY.first_name() # mypy: ignore - last_name = FACTORY.last_name() # mypy: ignore - email = FACTORY.email() # mypy: ignore - date_joined = FACTORY.date_time() # mypy: ignore - last_login = FACTORY.date_time() # mypy: ignore + username = FACTORY.username() # type: ignore + first_name = FACTORY.first_name() # type: ignore + last_name = FACTORY.last_name() # type: ignore + email = FACTORY.email() # type: ignore + date_joined = FACTORY.date_time() # type: ignore + last_login = FACTORY.date_time() # type: ignore is_superuser = False is_staff = False - is_active = FACTORY.pybool() # mypy: ignore + is_active = FACTORY.pybool() # type: ignore password = PreSave(set_password, password="test1234") # type: ignore group = PostSave(add_to_group, name="TestGroup1234") # type: ignore @@ -109,14 +109,14 @@ def _post_save_method(self, instance): class ArticleFactory(SQLAlchemyModelFactory): """Article factory.""" - title = FACTORY.sentence() # mypy: ignore - slug = FACTORY.slug() # mypy: ignore - content = FACTORY.text() # mypy: ignore - image = FACTORY.png_file(storage=STORAGE) # mypy: ignore - pub_date = FACTORY.date() # mypy: ignore - safe_for_work = FACTORY.pybool() # mypy: ignore - minutes_to_read = FACTORY.pyint(min_value=1, max_value=10) # mypy: ignore - author = SubFactory(UserFactory) # mypy: ignore + title = FACTORY.sentence() # type: ignore + slug = FACTORY.slug() # type: ignore + content = FACTORY.text() # type: ignore + image = FACTORY.png_file(storage=STORAGE) # type: ignore + pub_date = FACTORY.date() # type: ignore + safe_for_work = FACTORY.pybool() # type: ignore + minutes_to_read = FACTORY.pyint(min_value=1, max_value=10) # type: ignore + author = SubFactory(UserFactory) # type: ignore class Meta: model = Article diff --git a/examples/tortoise/README.rst b/examples/tortoise/README.rst new file mode 100644 index 0000000..1af57d2 --- /dev/null +++ b/examples/tortoise/README.rst @@ -0,0 +1,4 @@ +TortoiseORM +=========== +Example factories for models based on +`TortoiseORM `_. diff --git a/fake.py b/fake.py index 85e3f40..4435103 100644 --- a/fake.py +++ b/fake.py @@ -2523,9 +2523,9 @@ def get_provider_for_type(cls, field_type) -> Optional[Callable]: return None if ( hasattr(field_type, "__origin__") - and field_type.__origin__ is Optional + and field_type.__origin__ is Optional # noqa ): - field_type = field_type.__args__[0] + field_type = field_type.__args__[0] # noqa return cls.TYPE_TO_PROVIDER.get(field_type) @classmethod