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

[Regression] TypeVar crashes when name is specified via keyword parameters #110782

Closed
mxschmitt opened this issue Oct 12, 2023 · 7 comments
Closed
Assignees
Labels
topic-typing type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@mxschmitt
Copy link

mxschmitt commented Oct 12, 2023

Crash report

What happened?

from typing import TypeVar
TypeVar(name="Handler")

Good: 3.11.X
Bad: 3.12.0

Downstream issue: jfhbrook/pyee#134

CPython versions tested on:

3.12

Operating systems tested on:

Linux, macOS

Output from running 'python -VV' on the command line:

Python 3.12.0 (main, Oct 12 2023, 00:42:11) [GCC 12.2.0]

Linked PRs

@mxschmitt mxschmitt added the type-crash A hard crash of the interpreter, possibly with a core dump label Oct 12, 2023
@JelleZijlstra JelleZijlstra self-assigned this Oct 12, 2023
@mxschmitt
Copy link
Author

Investigation: Changing TypeVar(name="Handler") to TypeVar("Handler") fixes it for me.

@JelleZijlstra
Copy link
Member

This is possibly a bug in Argument Clinic (see

typevar.__new__ as typevar_new
): if name is passed as a positional argument and there are no constraints, constraints is an empty tuple, but if it's a keyword argument, constraints is NULL.

I'll send a PR in a moment with a workaround in typevarobject.c.

@JelleZijlstra
Copy link
Member

Fix is merged, and will be in 3.12 soon; 3.12.1 should include the fix.

I'll note that using a keyword argument for the name of a TypeVar is not an officially supported part of the type system, so to make your code portable across type checkers and Python versions, you should use a positional argument for the name. It appears that pyright supports the keyword argument form, but mypy does not. We'll continue to support the keyword argument form at runtime for backwards compatibility.

JelleZijlstra added a commit that referenced this issue Oct 12, 2023
…args (GH-110784) (#110787)

gh-110782: Fix crash when TypeVar is constructed with keyword args (GH-110784)
(cherry picked from commit d2a536b)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@sobolevn
Copy link
Member

sobolevn commented Oct 14, 2023

Thanks!

@JelleZijlstra would you accept

*constraints: object
*

to be changed to just

*constraints

?

Because this seems like a fortunate event that this notation works. I was not able to find that this is a documented approach.

If so, I would send a PR :)

@AlexWaygood
Copy link
Member

Because this seems like a fortunate event that this notation works. I was not able to find that this is a documented approach.

We should try to make sure AC raises an error if it encounters syntax like this which isn't supported! Cc. @erlend-aasland

@sobolevn
Copy link
Member

Ok, will do after #110864

@JelleZijlstra
Copy link
Member

Thanks @sobolevn for investigating further!

I'm fine with whatever syntax works here, but ideally AC should support annotations on *args (e.g., I should be able to write *args: bool to get any number of bool args), and as Alex says it should error on unsupported constructs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

4 participants