Evaluating Pyright vs Mypy for preexisting codebase with too-dynamic model base class #8031
-
Hi! I'm about to make a pitch for introducing a type checker into a decently-sized preexisting Python codebase. I was going to go with Mypy because I've used it before, but am considering Pyright too. The main technical obstacle I'm expecting to face with this rollout has to do with the base class the codebase uses for its models. It's meant to be used like this: class FooModel(BaseModel):
def __init__(self) -> None:
super().__init__()
self._db_name = KeywordType()
self._db_is_something = BoolType()
self._db_a_set = SetType()
self._db_an_int = IntegerType()
self._db_a_float = FloatType()
self._db_a_dict = JSONType()
self._db_long_string = TextType()
self._db_a_list = ListType()
self.add_properties()
x = FooModel()
# The following lines of code work fine at runtime, but any sane type checker will complain about them.
x.name = "foo"
print(x.is_something)
sorted(x.a_list)
print(x.a_dict["foo"])
x.a_set.add(5) There is a ton of code that looks like this. It's not unheard-of for models to have dozens of On the Mypy side, I'm hoping that it'll be possible to write a plugin to make code like this type check. It'll be a pain to have to write a plugin and keep it up to date with new mypy versions and make it available to multiple repos that use this model system, but it seems potentially possible. I haven't looked into this in earnest yet, though. Since Pyright doesn't support plugins, I'm expecting that Pyright won't be an option for this codebase. I've heard great things about it, though, so I wanted to ask here: is it possible for us to use Pyright in a codebase that has lots and lots of code that looks like the snippet above? If so, how can I get that code to type check? (I guess another option would be to find a way to declare that these models are effectively |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Pyright is a standards-based type checker, and it supports all of the capabilities of the Python static type system. Let's see if there's a way to meet your needs within the capabilities of the type system. What library are you using here? I'm not sure where the symbols |
Beta Was this translation helpful? Give feedback.
Without more details about how the library works, it's difficult to offer any concrete suggestions, but I can offer some general ideas that might be useful to you.
BaseModel
derive fromAny
(conditionally, ifTYPE_CHECKING
is True). This will tell static type checkers to allow access (read or write) to any additional attributes onBaseModel
subclasses and treat these attributes asAny
.__getattr__
method in theBaseModel
class (conditionally, ifTYPE_CHECKING
is True). This gives you a bit more control than the previous option because you can overload it for specific attributes (usingLiteral
names). It also allows you to make any additional att…