PEP 612: Edits requested from typing-sig (#1545)

* x_int_y_str -> x_y y_int_x_str -> y_x

* Behavioral subtype

* documenting construction of user-defined generic classes
This commit is contained in:
Mark Mendoza 2020-07-31 13:23:39 -07:00 committed by GitHub
parent 3c44c60f04
commit f5d19f3628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 7 deletions

View File

@ -280,20 +280,40 @@ but is not obligated to do so.
def foo(x: Callable[P, int], y: Callable[P, int]) -> Callable[P, bool]: ... def foo(x: Callable[P, int], y: Callable[P, int]) -> Callable[P, bool]: ...
def x_int_y_str(x: int, y: str) -> int: ... def x_y(x: int, y: str) -> int: ...
def y_int_x_str(y: int, x: str) -> int: ... def y_x(y: int, x: str) -> int: ...
foo(x_int_y_str, x_int_y_str) # Should return (x: int, y: str) -> bool foo(x_y, x_y) # Should return (x: int, y: str) -> bool
foo(x_y, y_x) # Could return (__a: int, __b: str) -> bool
# This works because both callables have types that are
# behavioral subtypes of Callable[[int, str], int]
foo(x_int_y_str, y_int_x_str) # Could return (__a: int, __b: str) -> bool
# This works because both callables have types
# that are behavioral subtypes of
# Callable[[int, str], object]
def keyword_only_x(*, x: int) -> int: ... def keyword_only_x(*, x: int) -> int: ...
def keyword_only_y(*, y: int) -> int: ... def keyword_only_y(*, y: int) -> int: ...
foo(keyword_only_x, keyword_only_y) # Rejected foo(keyword_only_x, keyword_only_y) # Rejected
The constructors of user-defined classes generic on ``ParamSpec``\ s should be
evaluated in the same way.
.. code-block::
U = TypeVar("T")
class Y(Generic[U, P]):
f: Callable[P, str]
prop: U
def __init__(self, f: Callable[P, str], prop: U) -> None:
self.f = f
self.prop = prop
def a(q: int) -> str: ...
Y(a, 1) # Should resolve to Y[(q: int), int]
Y(a, 1).f # Should resolve to (q: int) -> str
The semantics of ``Concatenate[X, Y, P]`` are that it represents the parameters The semantics of ``Concatenate[X, Y, P]`` are that it represents the parameters
represented by ``P`` with two positional-only parameters prepended. This means represented by ``P`` with two positional-only parameters prepended. This means
that we can use it to represent higher order functions that add, remove or that we can use it to represent higher order functions that add, remove or