You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the @pydantic_settings.with_attrs_docs decorator, this should not affect type inference. (The decorator should just parse documentation comments into pydantic field descriptions.)
Actual behavior
VS Code reports problems with correct code (see example below), and autocompletion of the respective dataclass members does not work anymore.
The problem seems to be the if statement in the decorator being incorrectly handled; I put a more minimal example below as well.
frompydanticimportBaseModelfrompydantic_settingsimportwith_attrs_docs@with_attrs_docs. # <==== removing this line fixes the problemsclassFoo(BaseModel):bar: str"""here is docs"""#: docs for bazbaz: int#: yes#: of courseis_there_multiline: bool=Truefoo=Foo(bar='baz', baz=42) # <==== notice that this line is incorrectly reported as error
Here is a reduced testcase without pydantic or pydantic_settings:
The problem is that the pydantic_settings model is not properly annotating the decorator with_attrs_docs. It does not provide a return type annotation, so pyright (the type checker that underlies pylance) attempts to infer the return type. But return type inference is fallible.
The solution is to file a bug for the maintainers of pydantic_settings and have them properly annotate the with_attrs_docs decorator function. It will need to include generic types (a TypeVar bound to AnyPydanticModel) and overloads to hand the None case appropriately.
Thanks for looking into this. Your explanation makes sense to me; I think "return type inference is fallible" is a key statement here. I will follow up the the pydantic_settings project.
Environment data
Pylance language server 2022.4.0 (pyright 67ee16be)
Python 3.9.12 installed via MacPorts
Version: 1.66.1
Commit: 8dfae7a5cd50421d10cd99cb873990460525a898
Date: 2022-04-06T14:49:52.288Z (5 days ago)
Electron: 17.2.0
Chromium: 98.0.4758.109
Node.js: 16.13.0
V8: 9.8.177.11-electron.0
OS: Darwin arm64 21.3.0
Expected behavior
When using the @pydantic_settings.with_attrs_docs decorator, this should not affect type inference. (The decorator should just parse documentation comments into pydantic field descriptions.)
Actual behavior
VS Code reports problems with correct code (see example below), and autocompletion of the respective dataclass members does not work anymore.
The problem seems to be the
if
statement in the decorator being incorrectly handled; I put a more minimal example below as well.Logs
pylance_output.txt
Code Snippet / Additional information
This example is copied from the pydantic_settings documentation:
Here is a reduced testcase without pydantic or pydantic_settings:
The text was updated successfully, but these errors were encountered: