question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support type hinting?

See original GitHub issue

I can’t seem to have PyCharm play nice with the objects generated by Factory Boy; it would be nice to either automatically support type hinting somehow (ideally, it would simply be the model defined as a base for the factory) or at least add some documentation to explain how to have type checkers understand that, after p = PersonFactory(), variuable p is actually an instance of Person rather than of PersonFactory.

(Note: I’m not submitting a PR because I haven’t figured out a solution)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:57
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

46reactions
kamilglodcommented, Jan 13, 2021

my workaround for missing General protocol is as follow:

T = TypeVar("T")


class BaseFactory(Generic[T], factory.Factory):
    @classmethod
    def create(cls, **kwargs) -> T:
        return super().create(**kwargs)


class RoleFactory(BaseFactory[Role]):
    class Meta:
        model = Role

    id = factory.Sequence(lambda n: n)
    name = factory.Sequence(lambda n: f"Role {n}")

one drawback of this solution is that you always need to use Factory.create() instead of Factory() as it’s not possible (at least I didn’t find a proper solution) to override __new__() method with TypeVar.

23reactions
michaeloliverxcommented, Aug 13, 2020

Typing support would be really nice!

Read more comments on GitHub >

github_iconTop Results From Across the Web

typing — Support for type hints — Python 3.11.1 documentation
This module provides runtime support for type hints. The most fundamental support consists of the types Any , Union , Callable , TypeVar...
Read more >
Type hinting in PyCharm - JetBrains
PyCharm supports type hinting in function annotations and type comments using the typing module and the format defined by PEP 484. Adding type ......
Read more >
Type hints cheat sheet - mypy 0.991 documentation
Type hints cheat sheet#. This document is a quick cheat sheet showing how to use type annotations for various common types in Python....
Read more >
Type Hinting - Real Python
So, what is type hinting? It's a formal solution to statically indicate the type of a value. Type hints will help document your...
Read more >
12 Beginner Concepts About Type Hints To Improve Your ...
1— Type annotations: a new syntax to support adding types. Type hints are performed using Python annotations (introduced since PEP 3107).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found