-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
False positive 'Invalid value for "Config.extra" [pydantic-config]' #9021
Comments
Hmm, thanks for reporting this. Seems like a bug, but a pretty minor one. Maybe @dmontagu has an idea for an easy fix... |
This has to do with how For reference, pyright will handle this correctly as |
@Viicos is right; suggested approach compatible with mypy: # test.py
from pydantic import BaseModel, ConfigDict
from typing_extensions import Literal
def condition() -> bool:
return True
_my_model_extra: Literal['ignore', 'forbid'] = "ignore" if condition() else "forbid"
class MyModel(BaseModel):
model_config = ConfigDict(extra=_my_model_extra) I haven't confirmed this works but I think it should. If it doesn't, I am more confident the following would work: from pydantic import BaseModel, ConfigDict
from typing_extensions import Literal
def condition() -> bool:
return True
def _get_my_model_extra() -> Literal['ignore', 'forbid']:
if condition():
return 'ignore'
else:
return 'forbid'
class MyModel(BaseModel):
model_config = ConfigDict(extra=_get_my_model_extra()) I'm going to close this issue because I think this is the best we can do, but let us know if none of these approaches work for you and we can reopen it. |
Thanks for the respone @dmontagu , I've tried both your suggestions and they both still give the same error. |
@Apakottur thanks for the info, I see the problem — the issue is that we have additional checks in the plugin itself. I think I'm going to make this check more lax so it only errors if you use a string or member and it isn't the right value. If you use a conditional expression etc. it just won't type check the value (I think that's hard to do in a plugin, but if anyone knows a good way I will be happy to incorporate it), but note that if you use |
Initial Checks
Description
Consider this code:
Running
mypy --config mypy.toml test.py
givesThe error is incorrect, since the value is a valid literal in both cases, whicever branch of the condition is executed.
Might be related to #3481.
Example Code
No response
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: