Skip to content

Commit 55dcbda

Browse files
author
Guido van Rossum
committed
Merge main. There was a conflict from PR #1372.
2 parents 12a31ce + 27ab644 commit 55dcbda

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

mypy/build.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,15 @@ def __init__(self,
970970
file_id = '__builtin__'
971971
path = find_module(file_id, manager.lib_path)
972972
if path:
973-
# In silent mode, don't import .py files.
973+
# In silent mode, don't import .py files, except from stubs.
974974
if (SILENT_IMPORTS in manager.flags and
975975
path.endswith('.py') and (caller_state or ancestor_for)):
976-
if id != 'builtins':
976+
# (Never silence builtins, even if it's a .py file;
977+
# this can happen in tests!)
978+
if (id != 'builtins' and
979+
not ((caller_state and
980+
caller_state.tree and
981+
caller_state.tree.is_stub))):
977982
if ALMOST_SILENT in manager.flags:
978983
if ancestor_for:
979984
self.skipping_ancestor(id, path, ancestor_for)

mypy/checkexpr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,8 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
16251625
"""
16261626
if (isinstance(actual, NoneTyp) or isinstance(actual, AnyType) or
16271627
isinstance(formal, AnyType) or isinstance(formal, TypeVarType) or
1628-
isinstance(formal, CallableType)):
1628+
isinstance(formal, CallableType) or
1629+
(isinstance(actual, Instance) and actual.type.fallback_to_any)):
16291630
# These could match anything at runtime.
16301631
return 2
16311632
if isinstance(actual, UnionType):

mypy/subtypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def visit_deleted_type(self, left: DeletedType) -> bool:
9595
return True
9696

9797
def visit_instance(self, left: Instance) -> bool:
98+
if left.type.fallback_to_any:
99+
return True
98100
right = self.right
99101
if isinstance(right, Instance):
100102
if left.type._promote and is_subtype(left.type._promote,

mypy/test/data/check-modules.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,21 @@ tmp/main.py:2: note: (This note courtesy of --almost-silent)
721721
tmp/foo/bar.py: note: Ancestor package 'foo' silently ignored
722722
tmp/foo/bar.py: note: (Using --silent-imports, submodule passed on command line)
723723
tmp/foo/bar.py: note: (This note brought to you by --almost-silent)
724+
725+
[case testStubImportNonStubWhileSilent]
726+
# cmd: mypy -m main
727+
[file main.py]
728+
# flags: silent-imports
729+
from stub import x # Permitted
730+
from other import y # Disallowed
731+
x + '' # Error here
732+
y + '' # But not here
733+
[file stub.pyi]
734+
from non_stub import x
735+
[file non_stub.py]
736+
x = 42
737+
[file other.py]
738+
y = 42
739+
[builtins fixtures/module.py]
740+
[out]
741+
tmp/main.py:4: error: Unsupported left operand type for + ("int")

mypy/test/data/check-overloading.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,19 @@ f(y=[''], x=0)() # E: "int" not callable
567567
a = f(y=[['']], x=0) # E: List item 0 has incompatible type List[str]
568568
a() # E: "int" not callable
569569
[builtins fixtures/list.py]
570+
571+
[case testOverloadWithDerivedFromAny]
572+
from typing import Any, overload
573+
Base = None # type: Any
574+
575+
class C:
576+
@overload
577+
def __init__(self, a: str) -> None: pass
578+
@overload
579+
def __init__(self, a: int) -> None: pass
580+
581+
class Derived(Base):
582+
def to_dict(self) -> C:
583+
return C(self) # fails without the fix for #1363
584+
C(Derived()) # fails without the hack
585+
C(Base()) # Always ok

0 commit comments

Comments
 (0)