Skip to content

mypy complains that "Need type annotation" for Faker fields #7

@anentropic

Description

@anentropic

I have factories like:

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

    username = factory.Sequence(lambda n: f"{_faker.user_name()}_{n:08}")
    first_name = factory.Faker("first_name")
    last_name = factory.Faker("last_name")
    email = factory.Faker("ascii_safe_email")

with mypy 0.982 I get:

$ mypy --show-error-codes .
blog/factories.py:28: error: Need type annotation for "first_name"  [var-annotated]
blog/factories.py:29: error: Need type annotation for "last_name"  [var-annotated]
blog/factories.py:30: error: Need type annotation for "email"  [var-annotated]

so all the factory.Faker fields have a problem

I can guess this is probably due to the way factory.Faker itself is implemented - the type of the faker depends on the value of the string arg and there's no way for type info from faker itself to flow through that interface.

I guess the only way for that to work would be to tediously define an @overload with Literal[<method name>] for each faker method? Maybe that's not even possible either.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions