8000 Function compatibility rewrite by sixolet · Pull Request #2521 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Function compatibility rewrite #2521

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

Merged
merged 18 commits into from
Dec 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Make the text descriptions of args more concise
  • Loading branch information
sixolet committed Dec 21, 2016
commit c8d4458c34419f888fe06ecadc9abe4d2ae66b9d
15 changes: 8 additions & 7 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
ARG_CONSTRUCTOR_NAMES = {
ARG_POS: "Arg",
ARG_OPT: "DefaultArg",
ARG_NAMED: "Arg",
ARG_NAMED_OPT: "DefaultArg",
ARG_NAMED: "NamedArg",
ARG_NAMED_OPT: "DefaultNamedArg",
ARG_STAR: "StarArg",
ARG_STAR2: "KwArg",
}
Expand Down Expand Up @@ -188,7 +188,9 @@ def format(self, typ: Type, verbosity: int = 0) -> str:
arg_strings = []
for arg_name, arg_type, arg_kind in zip(
func.arg_names, func.arg_types, func.arg_kinds):
if arg_kind == ARG_POS and arg_name is None or verbosity == 0:
if ( arg_kind == ARG_POS and arg_name is None or
verbosity == 0 and arg_kind in (ARG_POS, ARG_OPT) ):

arg_strings.append(
strip_quotes(
self.format(
Expand All @@ -199,13 +201,12 @@ def format(self, typ: Type, verbosity: int = 0) -> str:
if arg_kind in (ARG_STAR, ARG_STAR2):
arg_strings.append("{}({})".format(
constructor,
arg_name))
strip_quotes(self.format(arg_type))))
else:
arg_strings.append("{}('{}', {}, {})".format(
arg_strings.append("{}('{}', {})".format(
constructor,
arg_name,
strip_quotes(self.format(arg_type)),
arg_kind in (ARG_NAMED, ARG_NAMED_OPT)))
strip_quotes(self.format(arg_type))))

return 'Callable[[{}], {}]'.format(", ".join(arg_strings), return_type)
else:
Expand Down
22 changes: 11 additions & 11 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ hh = h
ff = gg
ff_nonames = ff
ff_nonames = f_nonames # reset
ff = ff_nonames # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[Arg('a', int, False), Arg('b', str, False)], None])
ff = ff_nonames # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[Arg('a', int), Arg('b', str)], None])
ff = f # reset
gg = ff # E: Incompatible types in assignment (expression has type Callable[[Arg('a', int, False), Arg('b', str, False)], None], variable has type Callable[[Arg('a', int, False), DefaultArg('b', str, False)], None])
gg = hh # E: Incompatible types in assignment (expression has type Callable[[Arg('aa', int, False), DefaultArg('b', str, False)], None], variable has type Callable[[Arg('a', int, False), DefaultArg('b', str, False)], None])
gg = ff # E: Incompatible types in assignment (expression has type Callable[[Arg('a', int), Arg('b', str)], None], variable has type Callable[[Arg('a', int), DefaultArg('b', str)], None])
gg = hh # E: Incompatible types in assignment (expression has type Callable[[Arg('aa', int), DefaultArg('b', str)], None], variable has type Callable[[Arg('a', int), DefaultArg('b', str)], None])

[case testSubtypingFunctionsArgsKwargs]
from typing import Any, Callable
Expand All @@ -127,8 +127,8 @@ ee_def = everything
ee_var = everything
ee_var = everywhere

ee_var = specific_1 # The difference between Callable[[...], blah] and one with a *args: Any, **kwargs: Any is that the ... goes loosely both ways.
ee_def = specific_1 # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[Any, Any], None])
ee_var = specific_1 # The difference between Callable[..., blah] and one with a *args: Any, **kwargs: Any is that the ... goes loosely both ways.
ee_def = specific_1 # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[StarArg(Any), KwArg(Any)], None])

[builtins fixtures/dict.pyi]

Expand All @@ -140,7 +140,7 @@ def g(a): pass
ff = f
gg = g
ff = g
gg = f # E: Incompatible types in assignment (expression has type Callable[[Any], Any], variable has type Callable[[Arg('a', Any, False)], Any])
gg = f # E: Incompatible types in assignment (expression has type Callable[[Any], Any], variable has type Callable[[Arg('a', Any)], Any])


[case testLackOfNamesImplicitAnyFastparse]
Expand All @@ -152,7 +152,7 @@ def g(a): pass
ff = f
gg = g
ff = g
gg = f # E: Incompatible types in assignment (expression has type Callable[[Any], Any], variable has type Callable[[Arg('a', Any, False)], Any])
gg = f # E: Incompatible types in assignment (expression has type Callable[[Any], Any], variable has type Callable[[Arg('a', Any)], Any])

[case testLackOfNames]
def f(__a: int, __b: str) -> None: pass
Expand All @@ -162,7 +162,7 @@ ff = f
gg = g

ff = g
gg = f # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[Arg('a', int, False), Arg('b', str, False)], None])
gg = f # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[Arg('a', int), Arg('b', str)], None])

[case testLackOfNamesFastparse]
# flags: --fast-parser
Expand All @@ -174,7 +174,7 @@ ff = f
gg = g

ff = g
gg = f # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[Arg('a', int, False), Arg('b', str, False)], None])
gg = f # E: Incompatible types in assignment (expression has type Callable[[int, str], None], variable has type Callable[[Arg('a', int), Arg('b', str)], None])

[case testFunctionTypeCompatibilityWithOtherTypes]
from typing import Callable
Expand Down Expand Up @@ -1412,7 +1412,7 @@ def g4(*, y: int) -> str: pass
f(g1)
f(g2)
f(g3)
f(g4) # E: Argument 1 to "f" has incompatible type Callable[[int], str]; expected Callable[..., int]
f(g4) # E: Argument 1 to "f" has incompatible type Callable[[NamedArg('y', int)], str]; expected Callable[..., int]

[case testCallableWithArbitraryArgsSubtypingWithGenericFunc]
from typing import Callable, TypeVar
Expand Down Expand Up @@ -1540,7 +1540,7 @@ def g(x, y): pass
def h(x): pass
def j(y): pass
f = h
f = j # E: Incompatible types in assignment (expression has type Callable[[Arg('y', Any, False)], Any], variable has type Callable[[Arg('x', Any, False)], Any])
f = j # E: Incompatible types in assignment (expression has type Callable[[Arg('y', Any)], Any], variable has type Callable[[Arg('x', Any)], Any])
f = g # E: Incompatible types in assignment (expression has type Callable[[Any, Any], Any], variable has type Callable[[Any], Any])

[case testRedefineFunction2]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ x = None # type: Callable[[int], None]
def f(*x: int) -> None: pass
def g(*x: str) -> None: pass
x = f
x = g # E: Incompatible types in assignment (expression has type Callable[[str], None], variable has type Callable[[int], None])
x = g # E: Incompatible types in assignment (expression has type Callable[[StarArg(str)], None], variable has type Callable[[int], None])
[builtins fixtures/list.pyi]
[out]

Expand Down
0