python / typing-council

Decisions by the Python Typing Council
41 stars 3 forks source link

Typing spec update for tuples #13

Closed erictraut closed 7 months ago

erictraut commented 7 months ago

I'd like to request that the TC consider adoption of a new chapter in the typing spec that spells out type checking behaviors related to tuples.

TC Sign-off

Links to PR & Discussion The PR can be found here. The latest draft incorporates feedback from PR reviews and the discussion.

The discussion can be found here.

Current Type Checker Behaviors The latest published version of pyright (1.1.348) mostly conforms to the spec, although it doesn't correctly handle some nested unpacked tuple types.

The latest published version of mypy (1.8.0) mostly conforms to the proposed spec, but it has some false negatives for invalid unpacked tuple types. These should be straightforward to fix and shouldn't represent any backward compatibility issue for mypy users.

I haven't done a detailed analysis of pyre or pytype, but one obvious change that they will need to make is with regard to the special casing for tuple[Any, ...] specified in the proposed spec change.

Controversial Issues I don't think there are any controversial issues with the latest draft of the proposed spec change.

JelleZijlstra commented 7 months ago

Thanks, I signed off. Linking to this post where I list some possibly confusing consequences of this decision: https://discuss.python.org/t/what-are-the-subtyping-rules-for-tuple-t/39837/77

gvanrossum commented 7 months ago

I signed off. GH did something weird, I hope I didn't accidentally sign off for @rchen152 ?

JelleZijlstra commented 7 months ago

Looks like Rebecca signed off for herself (in case you don't know: you can see the edit history if you click on the "edited by gvanrossum" gray text at the top of Eric's post).

rchen152 commented 7 months ago

Yes, I signed off for myself =) I edited the comment at the same time to fix the spelling of my name, which I guess might have caused the weirdness.

erictraut commented 7 months ago

to fix the spelling of my name

Oops. Apologies for misspelling your name. :(

rchen152 commented 7 months ago

to fix the spelling of my name

Oops. Apologies for misspelling your name. :(

No worries!

erictraut commented 7 months ago

@hauntsaninja, let me know if you have any concerns or questions.

hauntsaninja commented 7 months ago

Thanks! I had two comments on the PR.

Also mypy and pyright currently differ on whether this is allowed:

def takes_at_least_one(x: tuple[int, *tuple[int, ...]]): ...

def takes_var(x: tuple[int, ...]):
    takes_at_least_one(x)

Just wanted to confirm whether the spec says something about this case and I missed it.

erictraut commented 7 months ago

Also mypy and pyright currently differ on whether this is allowed

Mypy is correct here. This is a bug in pyright. I noticed it when writing the conformance tests. :) Here is the bug I filed in the pyright issue tracker.