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

In sources.py, define T as a covariant type variable. #554

Closed
wants to merge 1 commit into from

Conversation

lgfang
Copy link

@lgfang lgfang commented Feb 26, 2025

Previously, the type parameter was invariant, leading Pyright to report a type incompatibility error:

$ pyright test
...
"CliSettingsSource[MyOptions]" is not assignable to "CliSettingsSource[object]"
   Type parameter "T@CliSettingsSource" is invariant, but "MyOptions" is not
     the same as "object" (reportAssignmentType)
...

Here's the content of test.py:

from pydantic_settings import BaseSettings, EnvSettingsSource, CliSettingsSource
class OptionsBase:
    pass
class MyOptions(OptionsBase):
    pass
cli_source: CliSettingsSource[object] = CliSettingsSource[MyOptions](BaseSettings)

Previously, the type parameter was invariant, leading Pyright to report a type
incompatibility error:
```
$ pyright test
...
"CliSettingsSource[MyOptions]" is not assignable to "CliSettingsSource[object]"
   Type parameter "T@CliSettingsSource" is invariant, but "MyOptions" is not
     the same as "object" (reportAssignmentType)
...
```

Here's the content of `test.py`:
```
from pydantic_settings import BaseSettings, EnvSettingsSource, CliSettingsSource
class OptionsBase:
    pass
class MyOptions(OptionsBase):
    pass
cli_source: CliSettingsSource[object] = CliSettingsSource[MyOptions](BaseSettings)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant