Skip to content

Understandable error message for incompatible AnyStr arguments #3433

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 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2bc24f3
New message for anystr errors
quartox May 24, 2017
36e66de
Fix logic path of anystr args
quartox May 24, 2017
7816c4f
Add tests
quartox May 24, 2017
6c3e822
Add docstring
quartox May 24, 2017
4c408e7
Fix tests
quartox May 24, 2017
9ee35b7
Get all AnyStr failures
quartox May 24, 2017
066fde6
Clean up error message
quartox May 24, 2017
c108a58
Fix tests
quartox May 24, 2017
4a133bd
Check for any constrained type
quartox Jun 4, 2017
982f4b7
Revert "Check for any constrained type"
quartox Jun 4, 2017
436925e
Revert "Revert "Check for any constrained type""
quartox Jun 4, 2017
bcf86dd
Revert "Merge with mypy master"
quartox Jun 4, 2017
13dcb81
Fix typeshed
quartox Jun 4, 2017
079bff6
Fix spacing
quartox Jun 4, 2017
9aaec96
Merge branch 'master' of https://github.com/python/mypy into anystr
quartox Jun 11, 2017
5c2b024
Test checkout upstream/master
quartox Jun 11, 2017
2bf23ca
Fix inappropriate updates to files
quartox Jun 11, 2017
d280d91
Finish cleanup of inappropriate additions
quartox Jun 11, 2017
36e3de1
Merge branch 'master' of https://github.com/python/mypy into anystr
quartox Jul 15, 2017
533260d
Improve checking
quartox Jul 15, 2017
93a7d58
Sparser testing
quartox Jul 15, 2017
7465cf8
Use MessageBuilder to get type strings
quartox Jul 16, 2017
e88b7b5
Add indeces to AnyStr error message
quartox Aug 5, 2017
2495e33
Merge branch 'master' of https://github.com/python/mypy into anystr
quartox Aug 5, 2017
7c3dd7b
Small change to error message
quartox Aug 5, 2017
5d77828
Fix some test messages
quartox Aug 5, 2017
24bdbe0
Fix more error messages
quartox Aug 5, 2017
30f98ab
Remove brittle indeces of inferred object
quartox Aug 18, 2017
092f7a7
Merge branch 'master' of https://github.com/python/mypy into anystr
quartox Aug 18, 2017
36fdd0f
Remove mutliple indeces from tests
quartox Aug 19, 2017
b746fe8
Fix test typo
quartox Aug 19, 2017
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
Fix logic path of anystr args
  • Loading branch information
quartox committed Jun 5, 2017
commit 36e66dee04fd845fb6711270a37a8fa2447cb15e
27 changes: 25 additions & 2 deletions mypy/applytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import mypy.subtypes
from mypy.sametypes import is_same_type
from mypy.expandtype import expand_type
from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType
from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType, Instance
from mypy.messages import MessageBuilder
from mypy.nodes import Context

Expand Down Expand Up @@ -38,7 +38,10 @@ def apply_generic_arguments(callable: CallableType, types: List[Type],
types[i] = value
break
else:
if not msg.incompatible_anystr_arguments(callable, type, context):
arg_types = get_arg_types(callable)
if has_anystr_incompatible_args(arg_types, type):
msg.incompatible_anystr_arguments(callable, arg_types, context)
else:
msg.incompatible_typevar_value(callable, i + 1, type, context)
upper_bound = callable.variables[i].upper_bound
if (type and not isinstance(type, PartialType) and
Expand All @@ -62,3 +65,23 @@ def apply_generic_arguments(callable: CallableType, types: List[Type],
ret_type=expand_type(callable.ret_type, id_to_type),
variables=remaining_tvars,
)


def get_arg_types(callee: CallableType) -> List[str]:
arg_types = [] # type: List[str]
for arg_type in callee.arg_types:
if isinstance(arg_type, Instance):
arg_types.append(arg_type.type.name())
elif isinstance(arg_type, TypeVarType):
arg_types.append(arg_type.name)
else:
arg_types.append(str(arg_type))
return arg_types


def has_anystr_incompatible_args(arg_types: List[str], type: Type) -> bool:
if (arg_types.count('AnyStr') > 1 and isinstance(type, Instance) and
type.type.name() == 'object'):
return True
else:
return False
17 changes: 5 additions & 12 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,18 +819,11 @@ def incompatible_typevar_value(self, callee: CallableType, index: int,
self.fail('Type argument {} of {} has incompatible value {}'.format(
index, callable_name(callee), self.format(type)), context)

def incompatible_anystr_arguments(self, callee: CallableType, type: Type,
context: Context) -> bool:
arg_types = tuple(type.name for type in callee.arg_types)
if (arg_types.count('AnyStr') > 1 and isinstance(type, Instance) and
type.type.fullname() == 'builtins.object'):
self.fail(
'Type arguments of {} have incompatible values with expected {}'.format(
callable_name(callee), arg_types), context)
self.note('"AnyStr" arguments must be all "str" or all "byte"', context)
return True
else:
return False
def incompatible_anystr_arguments(self, callee: CallableType, arg_types: List[str],
context: Context) -> None:
self.fail('Type arguments of {} have incompatible values with expected {}'.format(
callable_name(callee), tuple(arg_types)), context)
self.note('"AnyStr" arguments must be all "str" or all "bytes"', context)

def overloaded_signatures_overlap(self, index1: int, index2: int,
context: Context) -> None:
Expand Down