-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Support type inference for defaultdict() #8167
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ class ellipsis: pass | |
# Primitive types are special in generated code. | ||
|
||
class int: | ||
@overload | ||
def __init__(self) -> None: pass | ||
@overload | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the change to mypyc needed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A test case relies on the |
||
def __init__(self, x: object, base: int = 10) -> None: pass | ||
def __add__(self, n: int) -> int: pass | ||
def __sub__(self, n: int) -> int: pass | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2976,3 +2976,97 @@ x: Optional[str] | |
y = filter(None, [x]) | ||
reveal_type(y) # N: Revealed type is 'builtins.list[builtins.str*]' | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testPartialDefaultDict] | ||
from collections import defaultdict | ||
x = defaultdict(int) | ||
x[''] = 1 | ||
reveal_type(x) # N: Revealed type is 'collections.defaultdict[builtins.str, builtins.int]' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a similar test case where value type is generic, e.g.: x = defaultdict(list) # No error
x['a'] = [1, 2, 3] and x = defaultdict(list) # Error here
x['a'] = [] |
||
|
||
y = defaultdict(int) # E: Need type annotation for 'y' | ||
|
||
z = defaultdict(int) # E: Need type annotation for 'z' | ||
z[''] = '' | ||
reveal_type(z) # N: Revealed type is 'collections.defaultdict[Any, Any]' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testPartialDefaultDictInconsistentValueTypes] | ||
from collections import defaultdict | ||
a = defaultdict(int) # E: Need type annotation for 'a' | ||
a[''] = '' | ||
a[''] = 1 | ||
reveal_type(a) # N: Revealed type is 'collections.defaultdict[builtins.str, builtins.int]' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testPartialDefaultDictListValue] | ||
# flags: --no-strict-optional | ||
from collections import defaultdict | ||
a = defaultdict(list) | ||
a['x'].append(1) | ||
reveal_type(a) # N: Revealed type is 'collections.defaultdict[builtins.str, builtins.list[builtins.int]]' | ||
|
||
b = defaultdict(lambda: []) | ||
b[1].append('x') | ||
reveal_type(b) # N: Revealed type is 'collections.defaultdict[builtins.int, builtins.list[builtins.str]]' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testPartialDefaultDictListValueStrictOptional] | ||
# flags: --strict-optional | ||
from collections import defaultdict | ||
a = defaultdict(list) | ||
a['x'].append(1) | ||
reveal_type(a) # N: Revealed type is 'collections.defaultdict[builtins.str, builtins.list[builtins.int]]' | ||
|
||
b = defaultdict(lambda: []) | ||
b[1].append('x') | ||
reveal_type(b) # N: Revealed type is 'collections.defaultdict[builtins.int, builtins.list[builtins.str]]' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testPartialDefaultDictSpecialCases] | ||
from collections import defaultdict | ||
class A: | ||
def f(self) -> None: | ||
self.x = defaultdict(list) | ||
self.x['x'].append(1) | ||
reveal_type(self.x) # N: Revealed type is 'collections.defaultdict[builtins.str, builtins.list[builtins.int]]' | ||
self.y = defaultdict(list) # E: Need type annotation for 'y' | ||
s = self | ||
s.y['x'].append(1) | ||
|
||
x = {} # E: Need type annotation for 'x' (hint: "x: Dict[<type>, <type>] = ...") | ||
x['x'].append(1) | ||
|
||
y = defaultdict(list) # E: Need type annotation for 'y' | ||
y[[]].append(1) | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testPartialDefaultDictSpecialCases2] | ||
from collections import defaultdict | ||
|
||
x = defaultdict(lambda: [1]) # E: Need type annotation for 'x' | ||
x[1].append('') # E: Argument 1 to "append" of "list" has incompatible type "str"; expected "int" | ||
reveal_type(x) # N: Revealed type is 'collections.defaultdict[Any, builtins.list[builtins.int]]' | ||
|
||
xx = defaultdict(lambda: {'x': 1}) # E: Need type annotation for 'xx' | ||
xx[1]['z'] = 3 | ||
reveal_type(xx) # N: Revealed type is 'collections.defaultdict[Any, builtins.dict[builtins.str, builtins.int]]' | ||
|
||
y = defaultdict(dict) # E: Need type annotation for 'y' | ||
y['x'][1] = [3] | ||
|
||
z = defaultdict(int) # E: Need type annotation for 'z' | ||
z[1].append('') | ||
reveal_type(z) # N: Revealed type is 'collections.defaultdict[Any, Any]' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testPartialDefaultDictSpecialCase3] | ||
from collections import defaultdict | ||
|
||
x = defaultdict(list) | ||
x['a'] = [1, 2, 3] | ||
reveal_type(x) # N: Revealed type is 'collections.defaultdict[builtins.str, builtins.list[builtins.int*]]' | ||
|
||
y = defaultdict(list) # E: Need type annotation for 'y' | ||
y['a'] = [] | ||
reveal_type(y) # N: Revealed type is 'collections.defaultdict[Any, Any]' | ||
[builtins fixtures/dict.pyi] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be cleaner to move this check to the below helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. It's here since we rely on the narrowed down type below, so we'd need a type check anyway.