Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cc8d33f
Add simple unbound typevar check and rudimentary test
Jul 17, 2022
cc8faff
Add test for unbound func returning iterables of TypeVars
Jul 17, 2022
a771393
Add appropriate error message and make the new test pass
Jul 17, 2022
714445e
add CollectArgTypes to get set of argument types
anilbey Jul 17, 2022
595ec8c
Merge branch 'add-unbound-typevar-check' of github.com:AurelienJaquie…
anilbey Jul 17, 2022
db501a9
lint fix
anilbey Jul 17, 2022
e31f967
fix type error
anilbey Jul 17, 2022
951a54d
extract check_unbound_return_typevar as method
anilbey Jul 17, 2022
b9b1561
check if return type is instantiated in typ.variables
anilbey Jul 17, 2022
0335cc1
Fix some tests that are affected by new implementation
Jul 17, 2022
0cf8e93
Merge branch 'add-unbound-typevar-check' of https://github.com/Aureli…
Jul 17, 2022
1ddf301
add testInnerFunctionTypeVar test
anilbey Jul 17, 2022
32c0c84
fix testErrorCodeNeedTypeAnnotation test
Jul 17, 2022
1175d53
add TYPE_VAR error code to unbound error failure
anilbey Jul 17, 2022
f1a27d1
Merge branch 'add-unbound-typevar-check' of https://github.com/Aureli…
Jul 17, 2022
402a4e3
Fix the tests
Jul 17, 2022
34dc475
move new tests in new test file
Jul 17, 2022
b78b926
add 'check-typevar-unbound.test' to testcheck
anilbey Jul 18, 2022
ce9ceaf
add more nested tests for unbound type
anilbey Jul 18, 2022
64b1834
microoptimise check_unbound_return_typevar check ret_type first
anilbey Jul 18, 2022
0ff4b12
Merge branch 'master' into add-unbound-typevar-check
ilevkivskyi Jul 18, 2022
4352bc3
add missing builtins fixture to failing test
anilbey Jul 18, 2022
abb6f1c
Merge branch 'add-unbound-typevar-check' of github.com:AurelienJaquie…
anilbey Jul 18, 2022
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
add CollectArgTypes to get set of argument types
  • Loading branch information
anilbey committed Jul 17, 2022
commit 714445ecd33453de2fcc9c2bca8f7ac91a716541
16 changes: 15 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
get_proper_types, is_literal_type, TypeAliasType, TypeGuardedType, ParamSpecType,
OVERLOAD_NAMES, UnboundType
)
from mypy.typetraverser import TypeTraverserVisitor
from mypy.sametypes import is_same_type
from mypy.messages import (
MessageBuilder, make_inferred_type_note, append_invariance_notes, pretty_seq,
Expand Down Expand Up @@ -919,7 +920,11 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
self.fail(message_registry.RETURN_TYPE_CANNOT_BE_CONTRAVARIANT,
typ.ret_type)

if not any([isinstance(argtype, TypeVarType) for argtype in typ.arg_types]):
arg_type_visitor = CollectArgTypes()
for argtype in typ.arg_types:
argtype.accept(arg_type_visitor)

if typ.ret_type not in arg_type_visitor.arg_types:
self.fail(message_registry.RETURN_TYPE_CANNOT_BE_CONTRAVARIANT,
typ.ret_type)

Expand Down Expand Up @@ -5866,6 +5871,15 @@ class Foo(Enum):
and member_type.fallback.type == parent_type.type_object())


class CollectArgTypes(TypeTraverserVisitor):
"""Collects the non-nested argument types in a set."""
def __init__(self) -> None:
self.arg_types: Set[Instance] = set()

def visit_type_var(self, t: TypeVarType) -> None:
self.arg_types.add(t)


@overload
def conditional_types(current_type: Type,
proposed_type_ranges: Optional[List[TypeRange]],
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,23 @@ def f() -> Iterable[T]: # no error
...

f()

[case testBoundTypeVar]
from typing import TypeVar

T = TypeVar('T')

def f(a: T, b: T) -> T: # raise error
...


[case testNestedBoundTypeVar]
from typing import Callable, Union, TypeVar

T = TypeVar('T')

def f(a: Union[int, T], b: str) -> T: # raise error
...

def g(a: Callable[..., T], b: str) -> T: # raise error
...