Skip to content

Fix TypeVar defaults with None (PEP 696) #16859

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

Merged
merged 1 commit into from
Feb 2, 2024

Conversation

cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Feb 2, 2024

Ref: #14851

@cdce8p cdce8p added the topic-pep-696 TypeVar defaults label Feb 2, 2024
Copy link
Contributor

github-actions bot commented Feb 2, 2024

Diff from mypy_primer, showing the effect of this PR on open source code:

steam.py (https://github.com/Gobot1234/steam.py)
- steam/badge.py:129: error: Argument 1 to "inventory" of "PartialUser" has incompatible type "tuple[str, int]"; expected "App[str]"  [arg-type]
+ steam/badge.py:129: error: Argument 1 to "inventory" of "PartialUser" has incompatible type "tuple[str, int]"; expected "App[str | None]"  [arg-type]
- steam/app.py:696: error: Argument 2 to "AppStats" has incompatible type "PartialApp[NameT]"; expected "App[str]"  [arg-type]
- steam/app.py:710: error: Argument 2 to "AppAchievement" has incompatible type "PartialApp[NameT]"; expected "PartialApp[str]"  [arg-type]
- steam/app.py:724: error: Type argument "Self" of "Leaderboard" must be a subtype of "App[str]"  [type-var]
- steam/app.py:736: error: Value of type variable "AppT" of "Leaderboard" cannot be "Self"  [type-var]
- steam/app.py:749: error: Type argument "Self" of "Leaderboard" must be a subtype of "App[str]"  [type-var]
- steam/app.py:765: error: Value of type variable "AppT" of "Leaderboard" cannot be "Self"  [type-var]
- steam/app.py:775: error: Type argument "Self" of "Leaderboard" must be a subtype of "App[str]"  [type-var]
- steam/app.py:786: error: Value of type variable "AppT" of "Leaderboard" cannot be "Self"  [type-var]
- steam/app.py:832: error: Argument 1 to "fetch_review" of "PartialUser" has incompatible type "PartialApp[NameT]"; expected "App[str]"  [arg-type]
- steam/app.py:1169: error: Type argument "Self" of "CommunityItem" must be a subtype of "PartialApp[str]"  [type-var]
- steam/app.py:1181: error: Value of type variable "AppT" of "CommunityItem" cannot be "Self"  [type-var]
- steam/app.py:1230: error: Type argument "Self" of "RewardItem" must be a subtype of "PartialApp[str]"  [type-var]
- steam/app.py:1240: error: Value of type variable "AppT" of "RewardItem" cannot be "Self"  [type-var]
- steam/app.py:1271: error: Value of type variable "AppT" of "AppBadge" cannot be "Self"  [type-var]
- steam/app.py:1300: error: Type argument "Self" of "RewardItem" must be a subtype of "PartialApp[str]"  [type-var]
- steam/app.py:1305: error: Type argument "Self" of "AppBadge" must be a subtype of "PartialApp[str]"  [type-var]
- steam/app.py:1316: error: Value of type variable "AppT" of "AppBadge" cannot be "Self"  [type-var]
- steam/app.py:1374: error: Argument 1 to "temporarily_play" of "ConnectionState" has incompatible type "PartialApp[NameT]"; expected "App[str]"  [arg-type]
- steam/profile.py:152: error: Argument 1 to "inventory" of "PartialUser" has incompatible type "tuple[str, int]"; expected "App[str]"  [arg-type]
+ steam/profile.py:152: error: Argument 1 to "inventory" of "PartialUser" has incompatible type "tuple[str, int]"; expected "App[str | None]"  [arg-type]
- steam/event.py:277: error: Incompatible types in assignment (expression has type "App[str] | PartialApp[str | None] | None", variable has type "PartialApp[str | None] | None")  [assignment]
+ steam/event.py:277: error: Incompatible types in assignment (expression has type "App[str | None] | None", variable has type "PartialApp[str | None] | None")  [assignment]
- steam/user_news.py:112: error: Argument 1 to "fetch_review" of "PartialUser" has incompatible type "PartialApp[str | None]"; expected "App[str]"  [arg-type]
- steam/ext/commands/commands.py:288: error: Incompatible types in assignment (expression has type "GroupMixin[Any] | Group[Any, [VarArg(Any), KwArg(Any)], Any] | None", variable has type "Self")  [assignment]
+ steam/ext/commands/commands.py:288: error: Incompatible types in assignment (expression has type "GroupMixin[Any | None] | Group[Any, [VarArg(Any), KwArg(Any)], Any] | None", variable has type "Self")  [assignment]
- steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str], *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str | None], *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str | None], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/csgo/models.py:179: note:          def inventory(self, App[str], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/csgo/models.py:179: note:          def inventory(self, App[str | None], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str | None], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/csgo/models.py:179: note:          def inventory(self, App[str], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/csgo/models.py:179: note:          def inventory(self, App[str | None], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/csgo/models.py:179: note:          def inventory(self, app: App[str | None], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str], *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str | None], *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str | None], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/tf2/client.py:36: note:          def inventory(self, App[str], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/tf2/client.py:36: note:          def inventory(self, App[str | None], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str | None], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/tf2/client.py:36: note:          def inventory(self, App[str], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/tf2/client.py:36: note:          def inventory(self, App[str | None], /, *, context_id: int | None = ..., language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
- steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]
+ steam/ext/tf2/client.py:36: note:          def inventory(self, app: App[str | None], *, language: Language | None = ...) -> Coroutine[Any, Any, Inventory[Item[ClientUser], ClientUser]]

@cdce8p cdce8p requested a review from JelleZijlstra February 2, 2024 14:52
@JelleZijlstra JelleZijlstra merged commit 3f58c2d into python:master Feb 2, 2024
@cdce8p cdce8p deleted the TypeVar-10-fix-strict-optional branch February 2, 2024 16:13
hauntsaninja pushed a commit that referenced this pull request Nov 27, 2024
Fixes #18188 

Currently it seems that `None` is dropped from type variable defaults
when assigning generic class to a type alias.

<details>
  <summary>Example</summary>

```python
from typing_extensions import TypeVar
from typing import Generic, Union

T1 = TypeVar("T1", default=Union[int, None])
T2 = TypeVar("T2", default=Union[int, None])


class A(Generic[T1, T2]):
    def __init__(self, a: T1, b: T2) -> None:
        self.a = a
        self.b = b


MyA = A[T1, int]
a: MyA = A(None, 10)  # error: Argument 1 to "A" has incompatible type "None"; expected "int"  [arg-type]
reveal_type(a.a)
```
</details>

The change is similar to #16859
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-pep-696 TypeVar defaults
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants