-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Accept Ellipsis
as last argument for Concatenate
.
#14656
Comments
Hi! This is a good idea, but I think we have to do some work before adding this to mypy.
Following from this, I think we need to update the PEP. I'm not sure about that "with [steering committee] approval" but PEP 1 is clear that when PEP 621 is put in a Final state, it should accurately reflect implementations (ie allowing To update the PEP, it seems we should run it by the author first... @mrkmndz are you fine with this change? You've probably said something about this before but I can't find it on a cursory look. |
PEP 612 is final, so it cannot be amended at this point. Mypy can deviate from the typing standards if its maintainers decide to do so, but if you want this change to be incorporated into the runtime, I'll point out that there is already a way to express this concept in the Python type system via a callback protocol. The proposal would offer a (slightly) terser way to express the same thing. It saves one line. class IntFun(Protocol):
def __call__(self, __x: int, *args: Any, **kwargs: Any) -> None: ... versus IntFun: TypeAlias = Callable[Concatenate[int, ...], None] Normally, the bar is quite high for introducing a redundant mechanism. You would need to demonstrate convincingly that this is a common enough pattern to justify it. |
Currently its status is Accepted; not Final :^) This is probably an oversight tbf, but it opens the gate for features like this.
Additionally, this already holds true at runtime (and stdlib docs) which is part of the way there! |
@erictraut That would be a good alternative solution. Currently it is unsupported by mypy. Mypy does support Maybe, in view of the addition of |
@randolf-scholz, mypy is correct in emitting an error in your example because function So you are looking for a way in the type system to represent a function that accepts a single positional argument that is type If this is really what you want, you could use the following: P = ParamSpec("P")
_IntFun: TypeAlias = Callable[Concatenate[int, P], None]
IntFun: TypeAlias = _IntFun[...] Mypy currently emits an error here, but it's legal according to PEP 612, so it appears to just be a bug. |
The point is to be able to represent partial signatures. This has been requested multiple times, so I think I am not doing something super arcane here. (#5876 (comment), #6077, #8263, python/typing#696) A common use-case are decorators that add/remove arguments, as described in https://peps.python.org/pep-0612/#motivation. Another use-case is when the remaining arguments are provided at-runtime from a config-file.
Well, if it is known that the first argument of a Callable must be int, but the remainder of the signature is unknown (including even the number of args!), then the type checker can still verify that whenever the function is called the first argument is |
Note that the following definition will not work with the following function: def test_func(arbitrary_arg: int, *args: str, **kwargs: Any) -> None:
return None You will have to specify def test_func(arbitrary_arg: int, /, *args: str, **kwargs: Any) -> None:
return None |
Sorry for spamming you with comments earlier, it was towards the end of work and I didn't read the issue properly. |
@Jacob-Flasheye, it's documented in the section titled Valid use locations. Here's the specific portion:
I recommend closing this issue. If someone wants to propose a change to the type system to support ellipsis, the mypy issue tracker isn't the right place for this. It should be discussed in the python/typing discussion forum. |
Should the Python docs be updated then? As they explicitly state (as OP mentioned) that |
cc @JelleZijlstra do you have context on what the intended state is here? |
The change to the CPython runtime behaviour and the typing docs was made in the CPython PR python/cpython#30969. That change was made after a lengthy discussion in the CPython issue python/cpython#88954 between the authors of PEP 612 and the A PEP is a historical document that represents the consensus of the community at a fixed point in time; where a PEP and the "living documentation" at CPython contradict each other, the living documentation always takes precedence. We don't have a great mechanism right now for tweaking typing specs without writing a whole new PEP, and it's arguable whether or not the discussion in python/cpython#88954 was visible enough to the community as a whole. As a practical matter, however, I think we should view the spec as having been amended following the discussion in python/cpython#88954. Mypy should therefore change its behaviour here, as it is not following the amended spec. |
Ah, thanks for the links to the threads. Since that has now been added to the spec, I'll add it to pyright as well. |
Fixes #14761 Fixes #15318 Fixes #14656 Fixes #13518 I noticed there is a bunch of inconsistencies in `semanal`/`typeanal` for ParamSpecs, so I decided do a small cleanup. Using this opportunity I also allow `Concatenate[int, ...]` (with literal Ellipsis), and reduce verbosity of some errors. cc @A5rocks
@randolf-scholz is the project you're running into this on open source? |
Bug Report
https://mypy-play.net/?mypy=master&python=3.11&gist=f4ba9e669549fae3b1a75e02af311db2
Expected Behavior
According to docs https://docs.python.org/3/library/typing.html#typing.Concatenate:
This change deviates from PEP 612 and was introduced in bpo-44791
The text was updated successfully, but these errors were encountered: