Skip to content
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
6 changes: 5 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3595,7 +3595,11 @@ def visit_complex_expr(self, e: ComplexExpr) -> Type:

def visit_ellipsis(self, e: EllipsisExpr) -> Type:
"""Type check '...'."""
return self.named_type("builtins.ellipsis")
try:
return self.named_type("types.EllipsisType")
except KeyError:
# In test cases 'types' may not be available or may be shadowed.
return AnyType(TypeOfAny.special_form)

def visit_op_expr(self, e: OpExpr) -> Type:
"""Type check a binary operator expression."""
Expand Down
1 change: 1 addition & 0 deletions mypy/test/testexportjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
"typing_extensions",
"sys",
"collections",
"types",
):
continue
fnam = os.path.join(cache_dir, f"{module}.data.ff")
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def final(func: _T) -> _T: ...
"""

stubtest_builtins_stub = """
import types
Comment thread
hamdanal marked this conversation as resolved.
from typing import Generic, Mapping, Sequence, TypeVar, overload
T = TypeVar('T')
Expand All @@ -121,7 +122,6 @@ class dict(Mapping[KT, VT]): ...
class frozenset(Generic[T]): ...
class function: pass
class ellipsis: pass
class int: ...
class float: ...
Expand Down
16 changes: 15 additions & 1 deletion mypy/test/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@

from __future__ import annotations

from mypy.nodes import AssignmentStmt, CallExpr, Expression, IntExpr, NameExpr, Node, TypeVarExpr
from mypy.nodes import (
AssignmentStmt,
CallExpr,
EllipsisExpr,
Expression,
IntExpr,
NameExpr,
Node,
TypeVarExpr,
)
from mypy.traverser import TraverserVisitor
from mypy.treetransform import TransformVisitor
from mypy.types import Type
Expand Down Expand Up @@ -37,6 +46,11 @@ def visit_int_expr(self, n: IntExpr) -> None:
self.nodes.add(n)
super().visit_int_expr(n)

def visit_ellipsis(self, n: EllipsisExpr) -> None:
if self.ignore_file:
self.nodes.add(n)
super().visit_ellipsis(n)


def ignore_node(node: Expression) -> bool:
"""Return True if node is to be omitted from test case output."""
Expand Down
3 changes: 1 addition & 2 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# test cases.

import _typeshed
import types
from typing import (
Self, TypeVar, Generic, List, Iterator, Iterable, Dict, Optional, Tuple, Any, Set,
overload, Mapping, Union, Callable, Sequence, FrozenSet, Protocol
Expand Down Expand Up @@ -57,8 +58,6 @@ def __new__(cls, *args: object) -> Any: ...
__name__ : str
__annotations__: Dict[str, Any]

class ellipsis: pass

# Primitive types are special in generated code.

class int:
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 @@ -461,8 +461,8 @@ def typeddict() -> Sequence[D]:
x = [{'x': 0}] # type: List[a.D]
return x # E: Incompatible return value type (got "list[a.D]", expected "Sequence[b.D]")

a = (a.A(), A())
a.x # E: "tuple[a.A, b.A]" has no attribute "x"
a = (a.A(), A()) # E: Incompatible types in assignment (expression has type "tuple[a.A, b.A]", variable has type Module)
Comment thread
hamdanal marked this conversation as resolved.
a.x # E: Module has no attribute "x"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -8593,7 +8593,7 @@ plain_var: int
class Foo:
import mod

reveal_type(Foo.mod) # N: Revealed type is "builtins.object"
reveal_type(Foo.mod) # N: Revealed type is "types.ModuleType"
reveal_type(Foo.mod.foo) # N: Revealed type is "builtins.int"
[file mod.py]
foo: int
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,7 @@ class Pet(Enum):
CAT = ...
DOG: str = ... # E: Enum members must be left unannotated \
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members \
# E: Incompatible types in assignment (expression has type "ellipsis", variable has type "str")
# E: Incompatible types in assignment (expression has type "EllipsisType", variable has type "str")
[builtins fixtures/enum.pyi]

[case testEnumValueWithPlaceholderNodeType]
Expand Down Expand Up @@ -2736,23 +2736,23 @@ class FromStub(Enum):
FOO = ...

reveal_type(FromStub.FOO) # N: Revealed type is "Literal[__main__.FromStub.FOO]?"
reveal_type(FromStub.FOO.value) # N: Revealed type is "builtins.ellipsis"
reveal_type(FromStub.FOO.value) # N: Revealed type is "types.EllipsisType"
reveal_type(FromStub.FOO._value_) # N: Revealed type is "builtins.int"

class InheritedStr(StrEnum):
FOO = ...

reveal_type(InheritedStr.FOO) # N: Revealed type is "Literal[__main__.InheritedStr.FOO]?"
reveal_type(InheritedStr.FOO.value) # N: Revealed type is "builtins.ellipsis"
reveal_type(InheritedStr.FOO._value_) # N: Revealed type is "builtins.ellipsis"
reveal_type(InheritedStr.FOO.value) # N: Revealed type is "types.EllipsisType"
reveal_type(InheritedStr.FOO._value_) # N: Revealed type is "types.EllipsisType"

class Wrapper:
class Nested(StrEnum):
FOO = ...

reveal_type(Wrapper.Nested.FOO) # N: Revealed type is "Literal[__main__.Wrapper.Nested.FOO]?"
reveal_type(Wrapper.Nested.FOO.value) # N: Revealed type is "builtins.ellipsis"
reveal_type(Wrapper.Nested.FOO._value_) # N: Revealed type is "builtins.ellipsis"
reveal_type(Wrapper.Nested.FOO.value) # N: Revealed type is "types.EllipsisType"
reveal_type(Wrapper.Nested.FOO._value_) # N: Revealed type is "types.EllipsisType"
[builtins fixtures/enum.pyi]

[case testStrEnumEqualityReachability]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1660,13 +1660,13 @@ class C(B):

a: A
if str():
a = ... # E: Incompatible types in assignment (expression has type "ellipsis", variable has type "A")
a = ... # E: Incompatible types in assignment (expression has type "EllipsisType", variable has type "A")
b = ...
c = ...
if str():
b = c
....__class__
....a # E: "ellipsis" has no attribute "a"
....a # E: "EllipsisType" has no attribute "a"

class A: pass
[builtins fixtures/dict-full.pyi]
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ x + "" # E: Unsupported operand types for + ("int" and "str")
import mod
mod.x + 0
mod.x + "" # E: Unsupported operand types for + ("int" and "str")
mod.y # E: "object" has no attribute "y"
mod + 0 # E: Unsupported left operand type for + ("object")
mod.y # E: Module has no attribute "y"
mod + 0 # E: Unsupported left operand type for + (Module)
[file mod.py]
1 + "" # E: Unsupported operand types for + ("int" and "str")
x = 0
Expand All @@ -565,8 +565,8 @@ from mod import x
x + "" # E: Unsupported operand types for + ("int" and "str")
import mod
mod.x + "" # E: Unsupported operand types for + ("int" and "str")
mod.y # E: "object" has no attribute "y"
mod + 0 # E: Unsupported left operand type for + ("object")
mod.y # E: Module has no attribute "y"
mod + 0 # E: Unsupported left operand type for + (Module)
[file mod.py]
1 + ""
x = 0
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-incomplete-fixture.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import m
# This used to cause a crash since types.ModuleType is not available
# by default. We fall back to 'object' now.
m.x # E: "object" has no attribute "x"
m.x # E: Module has no attribute "x"
[file m.py]

[case testSetMissingFromStubs]
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -5546,7 +5546,7 @@ import p.util
class N(p.util.Test):
...
[out2]
tmp/a.py:2: error: "object" has no attribute "N"
tmp/a.py:2: error: Module has no attribute "N"

[case testIncrementalIndirectSkipWarnUnused]
# flags: --follow-imports=skip --warn-unused-ignores
Expand Down Expand Up @@ -5801,7 +5801,7 @@ class C:
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[out2]
tmp/a.py:2: error: "object" has no attribute "xyz"
tmp/a.py:2: error: Module has no attribute "xyz"

[case testIncrementalInvalidNamedTupleInUnannotatedFunction]
# flags: --disable-error-code=annotation-unchecked
Expand Down Expand Up @@ -7553,9 +7553,9 @@ from pkg import submod
[file pkg/submod.pyi]
def foo() -> None: pass
[out]
tmp/a.py:3: error: "object" has no attribute "submod"
tmp/a.py:3: error: Module has no attribute "submod"
[out2]
tmp/a.py:3: error: "object" has no attribute "submod"
tmp/a.py:3: error: Module has no attribute "submod"

[case testIncrementalAccessSubmoduleWithoutExplicitImportNested]
import a
Expand Down Expand Up @@ -7608,9 +7608,9 @@ from pandas.core.dtypes.dtypes import X
[file pandas/core/dtypes/dtypes.py]
X = 0
[out]
tmp/a.py:6: error: "object" has no attribute "dtypes"
tmp/a.py:6: error: Module has no attribute "dtypes"
[out2]
tmp/a.py:2: error: "object" has no attribute "dtypes"
tmp/a.py:2: error: Module has no attribute "dtypes"

[case testStarImportCycleRedefinition]
import m
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-modules-fast.test
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ a = A()
[case testModuleLookupWeird]
# flags: --fast-module-lookup
from m import a
reveal_type(a) # N: Revealed type is "builtins.object"
reveal_type(a) # N: Revealed type is "types.ModuleType"
reveal_type(a.b) # N: Revealed type is "m.a.B"

[file m.py]
Expand Down
24 changes: 12 additions & 12 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,13 @@ x = ... # type: int

[case testEllipsisInitializerInStubFileWithoutType]
import m
m.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "ellipsis")
m.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "EllipsisType")
[file m.pyi]
# Ellipsis is only special with a # type: comment (not sure though if this is great)
x = ...

[case testEllipsisInitializerInModule]
x = ... # type: int # E: Incompatible types in assignment (expression has type "ellipsis", variable has type "int")
x = ... # type: int # E: Incompatible types in assignment (expression has type "EllipsisType", variable has type "int")

[case testEllipsisDefaultArgValueInStub]
import m
Expand All @@ -491,7 +491,7 @@ def f(x: int = ...) -> None: pass

[case testEllipsisDefaultParamValueInStub2]
import m
def f1(x: int = ...) -> int: return 1 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
def f1(x: int = ...) -> int: return 1 # E: Incompatible default for parameter "x" (default has type "EllipsisType", parameter has type "int")
def f2(x: int = '') -> int: return 1 # E: Incompatible default for parameter "x" (default has type "str", parameter has type "int")
[file m.pyi]
def g1(x: int = ...) -> int: pass
Expand All @@ -506,11 +506,11 @@ def ok_5(x: int = ...) -> None:
"""Docstring here"""
pass

def bad_1(x: int = ...) -> None: 1 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
def bad_2(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
def bad_1(x: int = ...) -> None: 1 # E: Incompatible default for parameter "x" (default has type "EllipsisType", parameter has type "int")
def bad_2(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "EllipsisType", parameter has type "int")
"""Docstring here"""
ok_1()
def bad_3(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
def bad_3(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "EllipsisType", parameter has type "int")
raise Exception("Some other exception")
[builtins fixtures/exception.pyi]

Expand All @@ -523,7 +523,7 @@ Both = Union[int, str]
def foo(x: int, y: int = ...) -> int: ...
@overload
def foo(x: str, y: str = ...) -> str: ...
def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for parameter "y" (default has type "ellipsis", parameter has type "int | str")
def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for parameter "y" (default has type "EllipsisType", parameter has type "int | str")
return x

@overload
Expand All @@ -543,14 +543,14 @@ class Wrap(Generic[T]): ...

class MyProtocol(Protocol):
def no_impl(self, x: Wrap[int] = ...) -> int: ...
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "Wrap[int]")
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "EllipsisType", parameter has type "Wrap[int]")

class MyAbstractClass:
@abstractmethod
def no_impl(self, x: Wrap[int] = ...) -> int: raise NotImplementedError

@abstractmethod
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "Wrap[int]")
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "EllipsisType", parameter has type "Wrap[int]")
[builtins fixtures/exception.pyi]

[case testStarImportOverlapping]
Expand Down Expand Up @@ -687,7 +687,7 @@ try:
except:
pass

import m as f # E: Incompatible import of "f" (imported name has type "object", local name has type "Callable[[Any], Any]")
import m as f # E: Incompatible import of "f" (imported name has type Module, local name has type "Callable[[Any], Any]")

[file m.py]
def f(x): pass
Expand All @@ -702,7 +702,7 @@ Y: Type[mod.B]
from mod import B as X
from mod import A as Y # E: Incompatible import of "Y" (imported name has type "type[A]", local name has type "type[B]")

import mod as X # E: Incompatible import of "X" (imported name has type "object", local name has type "type[A]")
import mod as X # E: Incompatible import of "X" (imported name has type Module, local name has type "type[A]")

[file mod.py]
class A: ...
Expand Down Expand Up @@ -2904,7 +2904,7 @@ from mystery import a, b as b, c as d # E: Cannot find implementation or librar
[case testPackagePath]
import p
reveal_type(p.__path__) # N: Revealed type is "builtins.list[builtins.str]"
p.m.__path__ # E: "object" has no attribute "__path__"
p.m.__path__ # E: Module has no attribute "__path__"

[file p/__init__.py]
from . import m as m
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-redefine.test
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ with B() as x:
import typing
try:
pass
except Exception as typing:
except Exception as typing: # E: Incompatible types in assignment (expression has type "Exception", variable has type Module)
pass
[builtins fixtures/exception.pyi]
[typing fixtures/typing-full.pyi]
Expand Down
1 change: 0 additions & 1 deletion test-data/unit/check-typeform.test
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,6 @@ class dict: pass
class str: pass
class type: pass
class tuple: pass
class ellipsis: pass
class BaseException: pass
class float: pass
[typing fixtures/typing-full.pyi]
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ x = 1
x = f()

[case testUnionWithEllipsis]
from types import EllipsisType
from typing import Union
def f(x: Union[int, EllipsisType]) -> int:
if x is Ellipsis:
reveal_type(x) # N: Revealed type is "builtins.ellipsis"
reveal_type(x) # N: Revealed type is "types.EllipsisType"
x = 1
reveal_type(x) # N: Revealed type is "builtins.int"
return x
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ reveal_type('') # No error here :-)

[case testUnreachableAfterToplevelAssertImport]
import foo
foo.bar() # E: "object" has no attribute "bar"
foo.bar() # E: Module has no attribute "bar"
[file foo.py]
import sys
assert sys.platform == 'lol'
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-vec.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[case testVecBasics]
from types import EllipsisType
from typing import Optional, Any, TypeVar

from librt.vecs import vec
Expand Down Expand Up @@ -30,7 +31,7 @@ vec_object: vec[object]

vec_bad_int: vec[int] # E: Invalid item type for "vec"
vec_bad_tuple: vec[tuple[int, str]] # E: Invalid item type for "vec"
vec_bad_union: vec[str | ellipsis] # E: Invalid item type for "vec"
vec_bad_union: vec[str | EllipsisType] # E: Invalid item type for "vec"
vec_bad_any: vec[Any] # E: Invalid item type for "vec"
vec_bad_two_args: vec[i32, i32] # E: Invalid item type for "vec"
vec_bad_optional1: vec[int | None] # E: Invalid item type for "vec"
Expand Down
Loading
Loading