gh-156233: Fix the prior author's 'overly_generic' example's Python SyntaxError, and type annotation errors - #157386
Draft
willy-b wants to merge 1 commit into
Draft
Conversation
…ror, and type annotation errors (ParamSpec default given as a tuple (instead of List), TypeVarTuple default specified as a plain packed tuple instead of unpacked) by prior author Re: the 'Compound Statement's 'overly_generic' example on https://docs.python.org/3.16/reference/compound_stmts.html (archived as is at https://web.archive.org/web/20260913010111/https://docs.python.org/3.16/reference/compound_stmts.html ) (and docs for earlier Python versions, e.g. 3.13, 3.14, 3.15), this commit: - Fixes Python 'SyntaxError': 'non-default type parameter \'TypeVarWithBound\' follows default type parameter for the overly_generic' by moving the 'TypeVarwithDefault' down in the list after the non-default type parameters (from https://github.com/python/cpython/blob/fe3a26f43fad1d6eed20172d7f63ee2931ae2ce1/Doc/reference/compound_stmts.rst?plain=1#L1852 ) - Fixes MyPy 'error: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec [misc]' by substituting '**SimpleParamSpec=[str, bytearray]' instead of the invalid '**SimpleParamSpec=[str, bytearray]' (fixing https://github.com/python/cpython/blob/fe3a26f43fad1d6eed20172d7f63ee2931ae2ce1/Doc/reference/compound_stmts.rst?plain=1#L1856 ) to follow guidance at e.g. https://typing.python.org/en/latest/spec/generics.html#paramspec-defaults (archived as is at https://web.archive.org/web/20260913004550/https://typing.python.org/en/latest/spec/generics.html#paramspec-defaults ) - Fixes MyPy 'error: The default argument to TypeVarTuple must be an Unpacked tuple [misc]' by replacing '*SimpleTypeVarTuple = (int, float),' with '*SimpleTypeVarTuple = *tuple[int, float],' (fixing https://github.com/python/cpython/blob/fe3a26f43fad1d6eed20172d7f63ee2931ae2ce1/Doc/reference/compound_stmts.rst?plain=1#L1855 ) to follow guidance at e.g. https://typing.python.org/en/latest/spec/generics.html#typevartuple-defaults (archived as is at https://web.archive.org/web/20260913004550/https://typing.python.org/en/latest/spec/generics.html#typevartuple-defaults ) - Fixes MyPy 'error: TypeVarTuple 'SimpleTypeVarTuple' is only valid with an unpack [valid-type]' by replacing '*e: SimpleTypeVarTuple,' with '*e: *SimpleTypeVarTuple,' to follow guidance at e.g. https://typing.python.org/en/latest/spec/generics.html#args-as-a-type-variable-tuple (archived as is at https://web.archive.org/web/20260913004550/https://typing.python.org/en/latest/spec/generics.html#args-as-a-type-variable-tuple )
Contributor
Author
|
Note that MyPy has its own bug that it cannot handle typed varargs after some other typed arguments like Callables preceding it, e.g. as I reported earlier in python/mypy#21907 , which one trying related examples may encounter (that exists prior to updating this example to use correct type annotations per the instructions at https://typing.python.org/en/latest/spec/generics.html , though I may subsequently try to fix that bug now that this example indirectly brought my attention to it ). |
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thanks so much Python team for everything you do. In discussion with @StanFromIreland at #156475 (comment) and @encukou at #156233 (comment) , I have opened this PR to fix the Compound Statement documentation page's 'overly_generic' example's Python SyntaxError, and type annotation errors (ParamSpec default given as a tuple (instead of List), TypeVarTuple default specified as a plain packed tuple instead of unpacked) by prior author.
Re: the 'Compound Statement's
overly_genericexample on https://docs.python.org/3.16/reference/compound_stmts.html (archived as is at https://web.archive.org/web/20260913010111/https://docs.python.org/3.16/reference/compound_stmts.html ) (and docs for earlier Python versions, e.g. 3.13, 3.14, 3.15), this commit makes minimal changes specific to this example only to:SyntaxError:non-default type parameter 'TypeVarWithBound' follows default type parameter for the overly_genericby moving theTypeVarwithDefaultdown in the list after the non-default type parameters (fromcpython/Doc/reference/compound_stmts.rst
Line 1852 in fe3a26f
error: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec [misc]by substituting**SimpleParamSpec=[str, bytearray]instead of the invalid**SimpleParamSpec=[str, bytearray](fixingcpython/Doc/reference/compound_stmts.rst
Line 1856 in fe3a26f
error: The default argument to TypeVarTuple must be an Unpacked tuple [misc]by replacing*SimpleTypeVarTuple = (int, float),with*SimpleTypeVarTuple = *tuple[int, float],(fixingcpython/Doc/reference/compound_stmts.rst
Line 1855 in fe3a26f
error: TypeVarTuple 'SimpleTypeVarTuple' is only valid with an unpack [valid-type]by replacing*e: SimpleTypeVarTuple,with*e: *SimpleTypeVarTuple,to follow guidance at e.g. https://typing.python.org/en/latest/spec/generics.html#args-as-a-type-variable-tuple (archived as is at https://web.archive.org/web/20260913004550/https://typing.python.org/en/latest/spec/generics.html#args-as-a-type-variable-tuple )See motivating discussion at #156233 (comment) and #156233 (comment) .
Should be backported from 3.16 through 3.13. Checked this example also on the lower version of Python 3.13.2 with MyPy 2.3.1 , PyreFly 1.3.0 , Pyright 1.1.414 .
(Re further backports: Note the version of the page on 3.12 did not have default type parameters but probably still needs a SEPARATE fix for
cpython/Doc/reference/compound_stmts.rst
Line 1718 in c016c25
SimpleTypeVarTupledoesn't have a default but is still used unpacked, so only one line needs to change)).