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

Remove unnecessary warning for config in plugin #9039

Merged
merged 2 commits into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion pydantic/mypy.py
Expand Up @@ -957,7 +957,11 @@ def get_config_update(self, name: str, arg: Expression) -> ModelConfigData | Non
elif isinstance(arg, MemberExpr):
forbid_extra = arg.name == 'forbid'
else:
error_invalid_config_value(name, self._api, arg)
# Do not emit an error for other types of `arg` (e.g., `NameExpr`, `ConditionalExpr`, etc.)
# It would be nice if we could type-check these better, but I don't know what the API is to
# evaluate an expr into its type and then check if that type is compatible with the expected type.
# Note that you can still get proper type checking via: `model_config = ConfigDict(...)`, it's just that
# if you don't use an explicit string, the plugin won't be able to infer whether extra is forbidden.
return None
return ModelConfigData(forbid_extra=forbid_extra)
if name == 'alias_generator':
Expand Down
9 changes: 9 additions & 0 deletions tests/mypy/modules/config_conditional_extra.py
@@ -0,0 +1,9 @@
from pydantic import BaseModel, ConfigDict


def condition() -> bool:
return True


class MyModel(BaseModel):
model_config = ConfigDict(extra='ignore' if condition() else 'forbid')
3 changes: 2 additions & 1 deletion tests/mypy/test_mypy.py
Expand Up @@ -87,7 +87,6 @@ def build(self) -> List[Union[Tuple[str, str], Any]]:
'plugin_fail.py',
'plugin_success_baseConfig.py',
'plugin_fail_baseConfig.py',
'plugin_optional_inheritance.py',
'pydantic_settings.py',
],
).build()
Expand All @@ -105,6 +104,8 @@ def build(self) -> List[Union[Tuple[str, str], Any]]:
# One-off cases
+ [
('mypy-plugin.ini', 'custom_constructor.py'),
('mypy-plugin.ini', 'config_conditional_extra.py'),
('mypy-plugin.ini', 'plugin_optional_inheritance.py'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved this down here to reduce the number of mypy tests by 1, since I added this yesterday and forgot I could add it down here instead of above to reduce the number of runs.

('mypy-plugin.ini', 'generics.py'),
('mypy-plugin.ini', 'root_models.py'),
('mypy-plugin-strict.ini', 'plugin_default_factory.py'),
Expand Down