Skip to content

Use lower case "list" and "dict" in invariance notes #18594

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 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3270,15 +3270,15 @@ def append_invariance_notes(
and expected_type.type.fullname == "builtins.list"
and is_subtype(arg_type.args[0], expected_type.args[0])
):
invariant_type = "List"
invariant_type = "list"
covariant_suggestion = 'Consider using "Sequence" instead, which is covariant'
elif (
arg_type.type.fullname == "builtins.dict"
and expected_type.type.fullname == "builtins.dict"
and is_same_type(arg_type.args[0], expected_type.args[0])
and is_subtype(arg_type.args[1], expected_type.args[1])
):
invariant_type = "Dict"
invariant_type = "dict"
covariant_suggestion = (
'Consider using "Mapping" instead, which is covariant in the value type'
)
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ from typing import List
x: List[int]
y: List[float]
y = x # E: Incompatible types in assignment (expression has type "List[int]", variable has type "List[float]") \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
[builtins fixtures/list.pyi]

Expand All @@ -388,7 +388,7 @@ from typing import Dict
x: Dict[str, int]
y: Dict[str, float]
y = x # E: Incompatible types in assignment (expression has type "Dict[str, int]", variable has type "Dict[str, float]") \
# N: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Mapping" instead, which is covariant in the value type
[builtins fixtures/dict.pyi]

Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2503,14 +2503,14 @@ from typing import Union, Dict, List
def f() -> List[Union[str, int]]:
x = ['a']
return x # E: Incompatible return value type (got "List[str]", expected "List[Union[str, int]]") \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant \
# N: Perhaps you need a type annotation for "x"? Suggestion: "List[Union[str, int]]"

def g() -> Dict[str, Union[str, int]]:
x = {'a': 'a'}
return x # E: Incompatible return value type (got "Dict[str, str]", expected "Dict[str, Union[str, int]]") \
# N: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Mapping" instead, which is covariant in the value type \
# N: Perhaps you need a type annotation for "x"? Suggestion: "Dict[str, Union[str, int]]"

Expand All @@ -2522,7 +2522,7 @@ def h() -> Dict[Union[str, int], str]:
def i() -> List[Union[int, float]]:
x: List[int] = [1]
return x # E: Incompatible return value type (got "List[int]", expected "List[Union[int, float]]") \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant

[builtins fixtures/dict.pyi]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ if int():
if int():
a = x3 \
# E: Incompatible types in assignment (expression has type "List[B]", variable has type "List[A]") \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
[builtins fixtures/list.pyi]
[typing fixtures/typing-medium.pyi]
Expand All @@ -1558,7 +1558,7 @@ if int():
if int():
a = x3 \
# E: Incompatible types in assignment (expression has type "List[B]", variable has type "List[A]") \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
[builtins fixtures/list.pyi]
[typing fixtures/typing-medium.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ a: List[Literal[1]]
b: List[Literal[1, 2, 3]]

foo(a) # E: Argument 1 to "foo" has incompatible type "List[Literal[1]]"; expected "List[Literal[1, 2]]" \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
foo(b) # E: Argument 1 to "foo" has incompatible type "List[Literal[1, 2, 3]]"; expected "List[Literal[1, 2]]"
bar(a)
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ asdf(x)
strict_optional = False
[out]
main:4: error: Argument 1 to "asdf" has incompatible type "List[str]"; expected "List[Optional[str]]"
main:4: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main:4: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main:4: note: Consider using "Sequence" instead, which is covariant
[builtins fixtures/list.pyi]

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ main:18: note: Revealed type is "builtins.str"
main:19: note: Revealed type is "Any"
main:20: note: Revealed type is "Union[builtins.int, builtins.str]"
main:21: error: Argument 1 to "foo" has incompatible type "List[bool]"; expected "List[int]"
main:21: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main:21: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main:21: note: Consider using "Sequence" instead, which is covariant
main:22: error: Argument 1 to "foo" has incompatible type "List[object]"; expected "List[int]"
main:23: error: Argument 1 to "foo" has incompatible type "List[Union[int, str]]"; expected "List[int]"
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if int():
s = f('')
o = f(1) \
# E: Incompatible types in assignment (expression has type "List[int]", variable has type "List[object]") \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
[builtins fixtures/list.pyi]

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def do_thing_with_enums(enums: Union[List[Enum], Enum]) -> None: ...

boop: List[Boop] = []
do_thing_with_enums(boop) # E: Argument 1 to "do_thing_with_enums" has incompatible type "List[Boop]"; expected "Union[List[Enum], Enum]" \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
[builtins fixtures/isinstancelist.pyi]

Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,12 @@ b = {'b': ['c', 'd']}
c = {'c': 1.0}
d = {'d': 1}
f(a) # E: Argument 1 to "f" has incompatible type "Dict[str, List[int]]"; expected "Dict[str, Sequence[int]]" \
# N: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Mapping" instead, which is covariant in the value type
f(b) # E: Argument 1 to "f" has incompatible type "Dict[str, List[str]]"; expected "Dict[str, Sequence[int]]"
g(c)
g(d) # E: Argument 1 to "g" has incompatible type "Dict[str, int]"; expected "Dict[str, float]" \
# N: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Mapping" instead, which is covariant in the value type
h(c) # E: Argument 1 to "h" has incompatible type "Dict[str, float]"; expected "Dict[str, int]"
h(d)
Expand All @@ -696,7 +696,7 @@ from typing import List, Union
def f(numbers: List[Union[int, float]]) -> None: pass
a = [1, 2]
f(a) # E: Argument 1 to "f" has incompatible type "List[int]"; expected "List[Union[int, float]]" \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
x = [1]
y = ['a']
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ y = [] # type: List[int]
x = y
[out]
bad.py:4: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[float]")
bad.py:4: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
bad.py:4: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
bad.py:4: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)

Expand Down