Skip to content
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

Revert "Revert use of ParamSpec for functools.wraps" #16942

Merged
merged 2 commits into from
Mar 13, 2024
Merged

Revert "Revert use of ParamSpec for functools.wraps" #16942

merged 2 commits into from
Mar 13, 2024

Conversation

tamird
Copy link
Contributor

@tamird tamird commented Feb 23, 2024

This is an experiment to see if we can remove

"0dd4b6f75", # ParamSpec for functools.wraps
and
"Revert use of `ParamSpec` for `functools.wraps`",
.

/cc @AlexWaygood

@jakkdl
Copy link

jakkdl commented Feb 23, 2024

This is an attempt to fix e.g. python/typeshed#10653

This comment has been minimized.

@AlexWaygood
Copy link
Member

Primer summary:

  • 10 new errors in user code
  • 1 existing error goes away
  • 4 existing errors have their error message change slightly

The next step is to go through the new errors and analyse whether they're true positives or false positives

@jakkdl
Copy link

jakkdl commented Feb 23, 2024

The ones in trio are true positives afaict @TeamSpen210

@TeamSpen210
Copy link
Contributor

Yeah, that block of code's not particularly typeable, the problem is inspect's functions causing typevars to be discarded. It might be fixable with TypeIs, actually. In any case it's not a problem for this PR.

@tamird
Copy link
Contributor Author

tamird commented Feb 26, 2024

@tamird
Copy link
Contributor Author

tamird commented Feb 28, 2024

What's next here?

@tamird tamird marked this pull request as ready for review March 11, 2024 13:01
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

jinja (https://github.com/pallets/jinja)
+ src/jinja2/compiler.py:56: error: Incompatible return value type (got "_Wrapped[[VarArg(Any), KwArg(Any)], Any, [VarArg(Any), KwArg(Any)], Any]", expected "F")  [return-value]

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/utilities/asyncutils.py:222: error: Argument 1 to "wraps" has incompatible type "T"; expected "Callable[..., object]"  [arg-type]
+ src/prefect/utilities/asyncutils.py:222: error: Argument 1 to "wraps" has incompatible type "T"; expected "Callable[[VarArg(Never), KwArg(Never)], Never]"  [arg-type]
+ src/prefect/utilities/asyncutils.py:264: error: Need type annotation for "wrapper"  [var-annotated]
- src/prefect/utilities/asyncutils.py:270: error: "Callable[[VarArg(Any), KwArg(Any)], Any]" has no attribute "aio"  [attr-defined]
+ src/prefect/utilities/asyncutils.py:270: error: "_Wrapped[Any, Any, [VarArg(Any), KwArg(Any)], Any]" has no attribute "aio"  [attr-defined]
- src/prefect/utilities/asyncutils.py:271: error: Incompatible return value type (got "Callable[[VarArg(Any), KwArg(Any)], Any]", expected "T")  [return-value]
+ src/prefect/utilities/asyncutils.py:271: error: Incompatible return value type (got "_Wrapped[Any, Any, [VarArg(Any), KwArg(Any)], Any]", expected "T")  [return-value]

tornado (https://github.com/tornadoweb/tornado)
+ tornado/gen.py:268: error: Unused "type: ignore" comment  [unused-ignore]

nox (https://github.com/wntrblm/nox)
+ nox/_decorators.py:54: error: Incompatible types in assignment (expression has type "_Wrapped[[VarArg(Any), KwArg(Any)], Any, [VarArg(Any), KwArg(Any)], Any]", variable has type "FunctionType")  [assignment]

trio (https://github.com/python-trio/trio)
+ src/trio/_core/_ki.py:142: error: Type of decorated function contains type "Any" ("_Wrapped[ArgsT, CoroutineType[Any, Any, Any], ArgsT, RetT]")  [misc]
+ src/trio/_core/_ki.py:152: error: Type of decorated function contains type "Any" ("_Wrapped[ArgsT, GeneratorType[Any, Any, Any], ArgsT, RetT]")  [misc]
+ src/trio/_core/_ki.py:168: error: Argument 1 to "wraps" has incompatible type "Union[Callable[ArgsT, AsyncGeneratorType[Any, Any]], Callable[..., AsyncGeneratorType[object, object]]]"; expected "Callable[[VarArg(object), KwArg(object)], AsyncGeneratorType[Any, Any]]"  [arg-type]
+ src/trio/_core/_ki.py:169: error: Type of decorated function contains type "Any" ("_Wrapped[object, AsyncGeneratorType[Any, Any], ArgsT, RetT]")  [misc]

jax (https://github.com/google/jax)
+ jax/_src/maps.py:627: error: Incompatible return value type (got "_Wrapped[[VarArg(Any), KwArg(Any)], Any, [VarArg(Any), KwArg(Any)], Any]", expected "Wrapped")  [return-value]
+ jax/_src/maps.py:627: note: "_Wrapped" is missing following "Wrapped" protocol member:
+ jax/_src/maps.py:627: note:     lower

bokeh (https://github.com/bokeh/bokeh)
+ src/bokeh/document/callbacks.py: note: In member "add_session_callback" of class "DocumentCallbackManager":
+ src/bokeh/document/callbacks.py:185:31: error: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "_Wrapped[[], None, [], None]")  [assignment]

ibis (https://github.com/ibis-project/ibis)
+ ibis/expr/types/relations.py:3037: error: Missing positional argument "predicates" in call to "__call__" of "_Wrapped"  [call-arg]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/wrapper/_implementations.py:503: error: Incompatible types in assignment (expression has type "Zen[P, R]", variable has type "Callable[[Any], Any]")  [assignment]
+ src/hydra_zen/wrapper/_implementations.py:503: error: Incompatible types in assignment (expression has type "Zen[P, R]", variable has type "_Wrapped[P, R, [Any], Any]")  [assignment]
- src/hydra_zen/wrapper/_implementations.py:503: note: "Zen[P, R].__call__" has type "Callable[[DataClass_ | type[DataClass_] | dict[Any, Any] | DictConfig | str], R]"

@JelleZijlstra
Copy link
Member

The bokeh hit looks unavoidable, the variable is assigned different types in two branches and mypy generally doesn't like that.

I suspect the ibis hit is because mypy thinks we have an unbound method wrapping a bound one (https://github.com/ibis-project/ibis/blob/7667328ada9cb8ef6bb4c456636c6a7ccab2ad1a/ibis/expr/types/joins.py#L274). Probably hard to avoid, but I'm also not sure why ibis needs the @wraps() call at all.

Overall, this feels like it's probably an improvement.

@tamird
Copy link
Contributor Author

tamird commented Mar 13, 2024

What do I need to do to get this landed?

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

I agree that the impact seems overall positive, and even if it was neutral, it's still better to stay closer to typeshed. Let's merge this.

@JukkaL JukkaL merged commit a00fcba into python:master Mar 13, 2024
18 checks passed
@tamird tamird deleted the functools.wraps-paramspec branch March 13, 2024 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants