Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commented Feb 5, 2022

Add methods typing_subst() in TypeVar and ParamSpec.

This is an alternative to #27511.

https://bugs.python.org/issue44796

Add methods __typing_subst__() in TypeVar and ParamSpec.
Copy link
Member

@JelleZijlstraJelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this does make things simpler, but we need to be careful that we don't break too many external consumers of typing code. Also, there may be interactions with #31021, which adds another TypeVar variant.

Here's a few comments, but I may need to look at this more to understand it better.

f"ParamSpec, or Concatenate. Got {t_args}")
returnsuper().__new__(cls, origin, args)

@property
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to keep __parameters__, it's de facto a public API by now.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is inherited from GenericAlias now.

Lib/typing.py Outdated
ifisinstance(arg, _SpecialForm) orargin (Generic, Protocol):
raiseTypeError(f"Plain {arg} is not valid as type argument")
ifisinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
ifisinstance(arg, (type, TypeVar, ForwardRef, types.UnionType)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this affect putting ParamSpec inside Annotated? Looks like we don't have tests for that yet.

Copy link
Contributor

@GBeauregardGBeauregardFeb 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into this.

This check is needed in order to allow instances of ParamSpec (i.e. P in P = ParamSpec("P")) to pass typing._type_check because callable(P) is False. This can show up for instance like Callable[Annotated[P, ""], T]. This line's change would regress the behavior to a runtime error.

n.b. this is moot if #31151 gets merged since this line is removed entirely

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callable[Annotated[P, ""], T] does not conform the PEP 612 specification.

callable ::= Callable "[" parameters_expression, type_expression "]" parameters_expression ::= | "..." | "[" [ type_expression ("," type_expression)* ] "]" | parameter_specification_variable | concatenate "[" type_expression ("," type_expression)* "," parameter_specification_variable "]" 

Copy link
Contributor

@GBeauregardGBeauregardFeb 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced it was the intent of the spec here to disallow Annotated, but you can also reach this code here:

fromtypingimportCallable, TypeVar, ParamSpec, get_type_hintsT=TypeVar("T") P=ParamSpec("P") defadd_logging(f: Callable["P", T]): passget_type_hints(add_logging)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I re-added ParamSpec. Now some errors will not be caught at runtime. We will return to this in future.

and_is_unpacked_typevartuple(old_arg)):
if_is_unpacked_typevartuple(old_arg):
original_typevartuple=old_arg.__parameters__[0]
new_arg=new_arg_by_param[original_typevartuple]
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not covered by tests.

It perhaps could be simplified too, but since it is not covered by tests I left it as is.

@serhiy-storchakaserhiy-storchaka marked this pull request as ready for review March 10, 2022 10:34
@serhiy-storchaka
Copy link
MemberAuthor

It now includes PEP 646 changes.

@gvanrossum
Copy link
Member

Did anyone review this?

@serhiy-storchaka
Copy link
MemberAuthor

Sorry, it was not reviewed after the last changes. But I addressed previous review comments. Is there something wrong with this code?

I merged this PR after short delay because it allows the C code for TypeVarTuple substitution (#31828) to be of reasonable complexity.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@serhiy-storchaka@gvanrossum@JelleZijlstra@GBeauregard@the-knights-who-say-ni@bedevere-bot