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
{{ message }}
Repository navigation
Commit 2534523
Browse filesBrowse the repository at this point in the historyBrowse files
Add informative notes to invariant function arguments (#3411)
Fixes#1115 and #3352 by providing a link to docs and a suggestion.
This currently only takes care of 'List' and 'Dict', which are the most typical for this kind of errors.
Copy file name to clipboardExpand all lines: test-data/unit/check-varargs.test
+45Lines changed: 45 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -593,3 +593,48 @@ class C:
593
593
def foo(self) -> None: pass
594
594
C().foo()
595
595
C().foo(1) # The decorator's return type says this should be okay
596
+
597
+
[case testInvariantDictArgNote]
598
+
from typing import Dict, Sequence
599
+
def f(x: Dict[str, Sequence[int]]) -> None: pass
600
+
def g(x: Dict[str, float]) -> None: pass
601
+
def h(x: Dict[str, int]) -> None: pass
602
+
a = {'a': [1, 2]}
603
+
b = {'b': ['c', 'd']}
604
+
c = {'c': 1.0}
605
+
d = {'d': 1}
606
+
f(a) # E: Argument 1 to "f" has incompatible type Dict[str, List[int]]; expected Dict[str, Sequence[int]] \
607
+
# N: "Dict" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance \
608
+
# N: Consider using "Mapping" instead, which is covariant in the value type
609
+
f(b) # E: Argument 1 to "f" has incompatible type Dict[str, List[str]]; expected Dict[str, Sequence[int]]
610
+
g(c)
611
+
g(d) # E: Argument 1 to "g" has incompatible type Dict[str, int]; expected Dict[str, float] \
612
+
# N: "Dict" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance \
613
+
# N: Consider using "Mapping" instead, which is covariant in the value type
614
+
h(c) # E: Argument 1 to "h" has incompatible type Dict[str, float]; expected Dict[str, int]
f(a) # E: Argument 1 to "f" has incompatible type List[int]; expected List[Union[int, float]] \
623
+
# N: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance \
624
+
# N: Consider using "Sequence" instead, which is covariant
625
+
x = [1]
626
+
y = ['a']
627
+
x = y # E: Incompatible types in assignment (expression has type List[str], variable has type List[int])
628
+
[builtins fixtures/list.pyi]
629
+
630
+
[case testInvariantTypeConfusingNames]
631
+
from typing import TypeVar
632
+
class Listener: pass
633
+
class DictReader: pass
634
+
def f(x: Listener) -> None: pass
635
+
def g(y: DictReader) -> None: pass
636
+
a = [1, 2]
637
+
b = {'b': 1}
638
+
f(a) # E: Argument 1 to "f" has incompatible type List[int]; expected "Listener"
639
+
g(b) # E: Argument 1 to "g" has incompatible type Dict[str, int]; expected "DictReader"
0 commit comments