You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
import logging
from typing import Annotated, Callable, ParamSpec, TypeVar
T = TypeVar("T")
P = ParamSpec("P")
def add_logging(f: Callable[P, T]) -> Callable[P, T]:
"""A type-safe decorator to add logging to a function."""
def inner(*args: Annotated[P.args, "meta"], **kwargs: P.kwargs) -> T:
logging.info(f"{f.__name__} was called")
return f(*args, **kwargs)
return inner
@add_logging
def add_two(x: float, y: float) -> float:
"""Add two numbers together."""
return x + y
This raises an error at runtime because P.args/P.kwargs cannot pass the typing._type_check called by Annotated because they are not callable(). This prevents being able to use Annotated on these type annotations.
This can be fixed by adding __call__ methods that raise to typing.ParamSpecArgs and typing.ParamSpecKwargs to match other typeforms.
Should it? Annotated wraps types, and P.args/P.kwargs are not types.
If make Annotated accepting P.args/P.kwargs (and possible other non-types, e.g. P), it is a new feature which should be publicly discussed first and documented. I do not thing it requires a PEP.
GBeauregard commented on Feb 8, 2022
GBeauregardmannequin
MannequinAuthor
More actions
My general understanding has been that since Annotated exists primarily to allow interoperability between typing and non-typing uses of annotation space, and that this is quite clear in the PEP, most cases where this is not allowed (especially at the top level of the annotation, as in this case) are either a bug or unintentional. We fixed Annotated wrapping Final and ClassVar a couple weeks ago for arguments sorta along these lines after discussion on typing-sig, where the concern of what constitutes a type to wrap was specifically addressed: https://bugs.python.org/issue46491
But, I am not opposed to discussing this case too, or discussing the issue more broadly. Is typing-sig the appropriate place for me to make a thread, or do we wait for comments here? Even if we decide this particular issue is okay to fix, I think you bring up a good point about documenting our decision clearly once and for all so I agree we should do this.
I wrote a PR that fixes the underlying issue here, but I'm leaving it as a draft while the discussion plays out. I think the stuff currently in the patch should be okay regardless of the discussion decision, because the underlying issue is that P.args and P.kwargs didn't pass typing._type_check, which is needed for reasons unrelated to Annotated (such as if they were stringified and had get_type_hints called on them).
If the result of the discussion is that we need to start supporting limitations on where Annotated is used, I'll add another PR that introduces support for this pattern in whatever locations that are decided it shouldn't be allowed.
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Activity
GBeauregard commentedon Feb 4, 2022
Consider the following.
This raises an error at runtime because P.args/P.kwargs cannot pass the typing._type_check called by Annotated because they are not callable(). This prevents being able to use Annotated on these type annotations.
This can be fixed by adding __call__ methods that raise to typing.ParamSpecArgs and typing.ParamSpecKwargs to match other typeforms.
I can write this patch given agreement
GBeauregard commentedon Feb 4, 2022
We can also fix this with my proposal in bpo-46644. I'm okay with either fix (that or implementing __call__), or both.
serhiy-storchaka commentedon Feb 8, 2022
Should it? Annotated wraps types, and P.args/P.kwargs are not types.
If make Annotated accepting P.args/P.kwargs (and possible other non-types, e.g. P), it is a new feature which should be publicly discussed first and documented. I do not thing it requires a PEP.
GBeauregard commentedon Feb 8, 2022
My general understanding has been that since Annotated exists primarily to allow interoperability between typing and non-typing uses of annotation space, and that this is quite clear in the PEP, most cases where this is not allowed (especially at the top level of the annotation, as in this case) are either a bug or unintentional. We fixed Annotated wrapping Final and ClassVar a couple weeks ago for arguments sorta along these lines after discussion on typing-sig, where the concern of what constitutes a type to wrap was specifically addressed: https://bugs.python.org/issue46491
But, I am not opposed to discussing this case too, or discussing the issue more broadly. Is typing-sig the appropriate place for me to make a thread, or do we wait for comments here? Even if we decide this particular issue is okay to fix, I think you bring up a good point about documenting our decision clearly once and for all so I agree we should do this.
GBeauregard commentedon Feb 8, 2022
I have made a thread on typing-sig to discuss this: https://mail.python.org/archives/list/typing-sig@python.org/thread/WZ4BCFO4VZ7U4CZ4FSDQNFAKPG2KOGCL/
GBeauregard commentedon Feb 10, 2022
I wrote a PR that fixes the underlying issue here, but I'm leaving it as a draft while the discussion plays out. I think the stuff currently in the patch should be okay regardless of the discussion decision, because the underlying issue is that P.args and P.kwargs didn't pass typing._type_check, which is needed for reasons unrelated to Annotated (such as if they were stringified and had get_type_hints called on them).
If the result of the discussion is that we need to start supporting limitations on where Annotated is used, I'll add another PR that introduces support for this pattern in whatever locations that are decided it shouldn't be allowed.
JelleZijlstra commentedon Mar 3, 2022
New changeset 75d2d94 by Gregory Beauregard in branch 'main':
bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238)
75d2d94
miss-islington commentedon Mar 3, 2022
New changeset 257f5be by Miss Islington (bot) in branch '3.10':
bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238)
257f5be
JelleZijlstra commentedon Mar 3, 2022
New changeset 59e1ce9 by Jelle Zijlstra in branch 'main':
bpo-46643: fix NEWS entry (GH-31651)
59e1ce9