8000 [WIP] Fix crash when joining tuple and NamedTuple by pkch · Pull Request #3129 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Fix crash when joining tuple and NamedTuple #3129

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

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' into namedtuple-join
  • Loading branch information
pkch committed Jun 5, 2017
commit 84b8fae3aafa032a2c407f73150f0fed82c60540
7 changes: 6 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2153,7 +2153,12 @@ def build_namedtuple_typeinfo(self, name: str, items: List[str], types: List[Typ
# Actual signature should return OrderedDict[str, Union[types]]
ordereddictype = (self.named_type_or_none('builtins.dict', [strtype, AnyType()])
or self.object_type())
fallback = self.named_type('__builtins__.tuple', [UnionType.make_simplified_union(types)])
# 'builtins.tuple' has only one type parameter.
#
# TODO: The corresponding type argument in the fallback instance should be a join of
# all item types, but we can't do joins during this pass of semantic analysis
# and we are using Any as a workaround.
fallback = self.named_type('__builtins__.tuple', [AnyType()])
# Note: actual signature should accept an invariant version of Iterable[UnionType[types]].
# but it can't be expressed. 'new' and 'len' should be callable types.
iterable_type = self.named_type_or_none('typing.Iterable', [AnyType()])
Expand Down
4 changes: 2 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ class Instance(Type):

def __init__(self, typ: mypy.nodes.TypeInfo, args: List[Type],
line: int = -1, column: int = -1, erased: bool = False) -> None:
assert(typ is None or typ.fullname() not in ["builtins.Any", "typing.Any"])
# TODO: assert(typ is None or typ.fullname() != 'builtins.tuple' or len(args) == 1)
# TODO: assert(typ is NOT_READY or typ.fullname() != 'builtins.tuple' or len(args) == 1)
assert(typ is NOT_READY or typ.fullname() not in ["builtins.Any", "typing.Any"])
self.type = typ
self.args = args
self.erased = erased
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fixtures/isinstancelist.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import builtinclass, Iterable, Iterator, TypeVar, List, Mapping, overload, Tuple, Set, Union, Sequence, Generic
from typing import Iterable, Iterator, TypeVar, List, Mapping, overload, Tuple, Set, Union, Sequence, Generic

class object:
def __init__(self) -> None: pass
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fixtures/list.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Builtins stub used in list-related test cases.

from typing import TypeVar, Generic, builtinclass, Iterable, Iterator, overload, Sequence
from typing import TypeVar, Generic, Iterable, Iterator, overload, Sequence

T = TypeVar('T')

Expand Down
28 changes: 27 additions & 1 deletion test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,33 @@ f.write('x')
f.write(b'x')
f.foobar()
[out]
_program.py:4: error: "IO" has no attribute "foobar"
_program.py:3: error: Argument 1 to "write" of "IO" has incompatible type "bytes"; expected "str"
_program.py:4: error: "TextIO" has no attribute "foobar"

[case testOpenReturnTypeInference]
reveal_type(open('x'))
reveal_type(open('x', 'r'))
reveal_type(open('x', 'rb'))
mode = 'rb'
reveal_type(open('x', mode))
[out]
_program.py:1: error: Revealed type is 'typing.TextIO'
_program.py:2: error: Revealed type is 'typing.TextIO'
_program.py:3: error: Revealed type is 'typing.BinaryIO'
_program.py:5: error: Revealed type is 'typing.IO[Any]'

[case testOpenReturnTypeInferenceSpecialCases]
reveal_type(open())
reveal_type(open(mode='rb', file='x'))
reveal_type(open(file='x', mode='rb'))
mode = 'rb'
reveal_type(open(mode=mode, file='r'))
[out]
_testOpenReturnTypeInferenceSpecialCases.py:1: error: Revealed type is 'typing.TextIO'
_testOpenReturnTypeInferenceSpecialCases.py:1: error: Too few arguments for "open"
_testOpenReturnTypeInferenceSpecialCases.py:2: error: Revealed type is 'typing.BinaryIO'
_testOpenReturnTypeInferenceSpecialCases.py:3: error: Revealed type is 'typing.BinaryIO'
_testOpenReturnTypeInferenceSpecialCases.py:5: error: Revealed type is 'typing.IO[Any]'

[case testGenericPatterns]
from typing import Pattern
Expand Down
2 changes: 1 addition & 1 deletion typeshed
Submodule typeshed updated 471 files
You are viewing a condensed version of this merge commit. You can view the full changes here.
0