Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate mypy cache if config changes #5007

Merged
merged 3 commits into from Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/5007-cdce8p.md
@@ -0,0 +1 @@
Invalidate mypy cache if plugin config changes
12 changes: 12 additions & 0 deletions pydantic/mypy.py
Expand Up @@ -42,6 +42,7 @@
FunctionContext,
MethodContext,
Plugin,
ReportConfigContext,
SemanticAnalyzerPluginInterface,
)
from mypy.plugins import dataclasses
Expand Down Expand Up @@ -98,6 +99,7 @@ def plugin(version: str) -> 'TypingType[Plugin]':
class PydanticPlugin(Plugin):
def __init__(self, options: Options) -> None:
self.plugin_config = PydanticPluginConfig(options)
self._plugin_data = self.plugin_config.to_data()
super().__init__(options)

def get_base_class_hook(self, fullname: str) -> 'Optional[Callable[[ClassDefContext], None]]':
Expand All @@ -124,6 +126,13 @@ def get_class_decorator_hook(self, fullname: str) -> Optional[Callable[[ClassDef
return dataclasses.dataclass_class_maker_callback # type: ignore[return-value]
return None

def report_config_data(self, ctx: ReportConfigContext) -> Dict[str, Any]:
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
"""Return all plugin config data.

Used by mypy to determine if cache needs to be discarded.
"""
return self._plugin_data

def _pydantic_model_class_maker_callback(self, ctx: ClassDefContext) -> None:
transformer = PydanticModelTransformer(ctx, self.plugin_config)
transformer.transform()
Expand Down Expand Up @@ -204,6 +213,9 @@ def __init__(self, options: Options) -> None:
setting = plugin_config.getboolean(CONFIGFILE_KEY, key, fallback=False)
setattr(self, key, setting)

def to_data(self) -> Dict[str, Any]:
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved
return {key: getattr(self, key) for key in self.__slots__}


def from_orm_callback(ctx: MethodContext) -> Type:
"""
Expand Down