gh-104050: Argument Clinic: Annotate the Block
class
#106519
Merged
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.
I tried long and hard to see if there was a sane way of annotating the
output
field of theBlock
class. In the end I concluded that, from a typing perspective, the current code is somewhat insane, so there's no sane way of doing it without substantially refactoring the code first 🙃 For now, I propose that we annotate it withAny
and leave it for another day.The issue is that
output
usually has typestr | None
, as stated by the docstring here:cpython/Tools/clinic/clinic.py
Lines 1659 to 1660 in 363f4f9
But whenever
self.block
is referenced from inside theDSLParser
class, it's of typelist[str]
(I'm pretty confidentself.block
is an instance ofBlock
):cpython/Tools/clinic/clinic.py
Lines 4471 to 4476 in 363f4f9
This is because
DSLParser.parse()
setsblock.output
to a list at the beginning of the function, and then sets it back to a string at the end of the function:cpython/Tools/clinic/clinic.py
Line 4502 in 363f4f9
cpython/Tools/clinic/clinic.py
Lines 4515 to 4518 in 363f4f9
Needless to say, this makes mypy furious if you give
Block.output
a type that isn'tAny
🙃