gh-156233: Fix the prior author's 'overly_generic' example's Python SyntaxError, and type annotation errors by willy-b · Pull Request #157386 · python/cpython · GitHub
Skip to content

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
python:mainfrom
willy-b:fix-156233-overly-generic-example
Draft

gh-156233: Fix the prior author's 'overly_generic' example's Python SyntaxError, and type annotation errors#157386
willy-b wants to merge 1 commit into
python:mainfrom
willy-b:fix-156233-overly-generic-example

Conversation

@willy-b

@willy-b willy-b commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

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_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 makes minimal changes specific to this example only to:

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

*d: SimpleTypeVarTuple,
in that branch to unpack the tuple (in that branch SimpleTypeVarTuple doesn't have a default but is still used unpacked, so only one line needs to change)).

(Note that no AI or LLM was used in the discovery of this issue or the development of this change.)

…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 )
@willy-b

willy-b commented Sep 13, 2026

Copy link
Copy Markdown
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 ).

@read-the-docs-community

Copy link
Copy Markdown

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

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant