Skip to content

Commit b522da8

Browse files
committed
Made Pydantic tests leaner; Removed extra variable in stubgen.
1 parent a7f95cd commit b522da8

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

mypy/stubgen.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,15 +861,13 @@ def visit_class_def(self, o: ClassDef) -> None:
861861
if self.analyzed and (spec := find_dataclass_transform_spec(o)):
862862
self.processing_dataclass = True
863863
self.dataclass_field_specifier = spec.field_specifiers
864-
is_pydantic_model = False
865864
for base_type_expr in o.base_type_exprs:
866865
if (
867866
isinstance(base_type_expr, (NameExpr, MemberExpr))
868867
and self.get_fullname(base_type_expr) == "pydantic.BaseModel"
869868
):
870-
is_pydantic_model = True
869+
self.processing_pydantic_model = True
871870
break
872-
self.processing_pydantic_model = is_pydantic_model
873871
super().visit_class_def(o)
874872
self.dedent()
875873
self._vars.pop()

test-data/unit/stubgen.test

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4756,15 +4756,17 @@ from _typeshed import Incomplete
47564756

47574757
def polar(*args, **kwargs) -> Incomplete: ...
47584758

4759-
47604759
[case testPydanticBaseModel]
47614760
import pydantic
4761+
from typing import Dict, List, Optional, Union
47624762

47634763
class User(pydantic.BaseModel):
47644764
id: int
47654765
name: str
47664766
active: bool = True
47674767
optional_field: str | None = None
4768+
tags: List[str] = []
4769+
properties: Dict[str, Union[str, int, float, bool]] = {}
47684770
[out]
47694771
import pydantic
47704772

@@ -4773,22 +4775,8 @@ class User(pydantic.BaseModel):
47734775
name: str
47744776
active: bool = ...
47754777
optional_field: str | None = ...
4776-
4777-
[case testPydanticBaseModelWithAnnotationsOnly]
4778-
import pydantic
4779-
4780-
class ConfigSettings(pydantic.BaseModel):
4781-
# Fields without initialization
4782-
db_name: str
4783-
port: int
4784-
debug: bool
4785-
[out]
4786-
import pydantic
4787-
4788-
class ConfigSettings(pydantic.BaseModel):
4789-
db_name: str
4790-
port: int
4791-
debug: bool
4778+
tags: list[str] = ...
4779+
properties: dict[str, str | int | float | bool] = ...
47924780

47934781
[case testPydanticNestedBaseModel]
47944782
from pydantic import BaseModel
@@ -4813,24 +4801,6 @@ class User(BaseModel):
48134801
age: int
48144802
address: Address | None = ...
48154803

4816-
[case testPydanticBaseModelComplex]
4817-
from pydantic import BaseModel
4818-
from typing import Dict, List, Optional, Union
4819-
4820-
class Item(BaseModel):
4821-
name: str
4822-
description: Optional[str] = None
4823-
tags: List[str] = []
4824-
properties: Dict[str, Union[str, int, float, bool]] = {}
4825-
[out]
4826-
from pydantic import BaseModel
4827-
4828-
class Item(BaseModel):
4829-
name: str
4830-
description: str | None = ...
4831-
tags: list[str] = ...
4832-
properties: dict[str, str | int | float | bool] = ...
4833-
48344804
[case testPydanticBaseModelInheritance]
48354805
from pydantic import BaseModel
48364806

@@ -4840,7 +4810,7 @@ class BaseUser(BaseModel):
48404810

48414811
class User(BaseUser):
48424812
name: str
4843-
email: str
4813+
email: str = '@'
48444814
[out]
48454815
from pydantic import BaseModel
48464816

@@ -4850,7 +4820,7 @@ class BaseUser(BaseModel):
48504820

48514821
class User(BaseUser):
48524822
name: str
4853-
email: str
4823+
email: str = ...
48544824

48554825
[case testPydanticModelWithMethods]
48564826
from pydantic import BaseModel
@@ -4860,11 +4830,11 @@ class User(BaseModel):
48604830
name: str
48614831

48624832
def get_display_name(self) -> str:
4863-
return f"User {self.name}"
4833+
return 'a'
48644834

48654835
@property
48664836
def display_id(self) -> str:
4867-
return f"ID: {self.id}"
4837+
return 'b'
48684838
[out]
48694839
from pydantic import BaseModel
48704840

0 commit comments

Comments
 (0)