-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Change invariant list
of subclassed AST
in ast fields, to covariant Sequence
#13942
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
Comments
Yes, this is a classic variance problem. But these objects are actually lists in AST produced by Python itself, so I don't think we should change the stubs. In your first example the solution is to declare the type as |
I don't understand what you mean. Or more likely, I didn't properly explain what I mean. I am talking about the objects passed to
Thank you for ensuring that I was aware of that solution. I don't have a different simple example or a merely complex example. I only have an absurdly complex example. My ast modules were originally developed in another package, and if you go to this commit in mapFolding, that is immediately before I switched to That code has significantly improved and is now in https://github.com/hunterhogan/astToolkit. "astToolkit/_toolGrab.py". mapFolding uses it... ... but I don't have enough technical knowledge to either 1) describe the situation properly or 2) show you a clear demonstration. Furthermore, it was only three days ago that I deciphered the documentation for :( This would require a lot of effort, so I don't think you should do it.
I think it all boils down to:
|
I suspect I do not have a sufficient understanding of the difference between
Maybe I should see that you are pointing out that the data is stored in a list, therefore the field type should be a list. This situation feels weirdly similar to ordering food in Xi'an. I only knew basic Beijing-er Mandarin, which is practically a foreign language in Xi'an. I tried to order beef dumplings and I got cabbage soup. I didn't have the words to properly differentiate my thoughts, and I couldn't hear pronunciation differences when she explained things to me. Well, maybe the jiaozi I want is for the class BoolOp(expr):
if sys.version_info >= (3, 10):
__match_args__ = ("op", "values")
op: boolop
values: list[expr]
if sys.version_info >= (3, 13):
def __init__(self, op: boolop, values: Sequence[expr] = ..., **kwargs: Unpack[_Attributes]) -> None: ...
else:
def __init__(self, op: boolop, values: Sequence[expr], **kwargs: Unpack[_Attributes]) -> None: ...
if sys.version_info >= (3, 14):
def __replace__(self, *, op: boolop = ..., values: Sequence[expr] = ..., **kwargs: Unpack[_Attributes]) -> Self: ... |
The problem with that is now something like |
Ah. If I knew how to read C, then I would have known that the method doesn't enforce the input type and allows tuples. I need to ruminate on this. |
If using a tuple instead of a list is a problem because of the type checker, but not because of the interpreter behavior, then a list and a tuple should have the same outcome. Since they don't have the same outcome, should we create an issue in github.com/python/cpython/?
|
In stdlib/ast.pyi, if a subclass of
ast.AST
has a field with a "collection" type, then the type isbuiltins.list
. In some cases, becauselist
is invariant, the feedback from type checkers is not helpful. Consider the following simple code.Mypy reports:
Pylance reports:
When I use a stub file that defines
body: Sequence[stmt]
in all the right places, the above code does not lead to diagnostic reports.Change
list
toSequence
for subclassed typesIf I understand correctly, even though
ast.Pass
is a subclass ofast.stmt
, the invariance oflist
requiresast.stmt
, not a subclass. In contrast, the covariance ofSequence
permits substituting subclasses for the base class. I'm not a trained programmer, and I don't have professional programming experience. In the past, when I have encountered this issue and got help from an AI assistant, the "solution" was alwaysThose kinds of statements have no value, increase complexity, and increase the risk of bugs.
If the solution is to change some
list
toSequence
in the stub file, then I happen to have already a list (no pun intended) of subclasses and fields.About the list contents:
list
if the type is subclassed, such aslist[ast.expr]
but notlist[ast.arg]
.list[str]
becausestr
could be subclassed, but I don't really have the knowledge to analyze its inclusion or exclusion.Coda
I replaced
list
withSequence
in an ast-related project, but my inexpert code might not be relevant to this conversation.The text was updated successfully, but these errors were encountered: