Skip to content
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

[stubtest] Test NamedTuple definitions with default fields #15774

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Changes from 2 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
38 changes: 38 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Mapping(Generic[_K, _V]): ...
class Match(Generic[AnyStr]): ...
class Sequence(Iterable[_T_co]): ...
class Tuple(Sequence[_T_co]): ...
class NamedTuple(tuple[Any, ...]): ...
def overload(func: _T) -> _T: ...
"""

Expand All @@ -82,6 +83,7 @@ def overload(func: _T) -> _T: ...
class object:
__module__: str
def __init__(self) -> None: pass
def __repr__(self) -> str: pass
class type: ...

class tuple(Sequence[T_co], Generic[T_co]): ...
Expand Down Expand Up @@ -1599,6 +1601,42 @@ class Y(TypedDict):
error=None,
)

@collect_cases
def test_named_tuple(self) -> Iterator[Case]:
yield Case(
stub="from typing import NamedTuple",
runtime="from typing import NamedTuple",
error=None,
)
yield Case(
stub="""
class X1(NamedTuple):
bar: int
foo: str = ...
""",
runtime="""
class X1(NamedTuple):
bar: int
foo: str = 'a'
""",
error=None,
)
yield Case(
stub="""
class X2(NamedTuple):
bar: int
foo: str
""",
runtime="""
class X2(NamedTuple):
bar: int
foo: str = 'a'
""",
# `__new__` will miss a default value for a `foo` parameter,
# but we don't generate special errors for `foo` missing `...` part.
error="X2.__new__",
)

@collect_cases
def test_type_var(self) -> Iterator[Case]:
yield Case(
Expand Down