Repository navigation
fix: ast.NameConstant missing fields - #14353
hunterhogan wants to merge 4 commits into
Conversation
hunterhogan
commented
Jul 1, 2025
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Is there something to change? |
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
|
I think it's not fully clear what the purpose of this change is per #14353 (comment) It may help if you could share an example of some code that currently doesn't pass type checking but which would after making this change. |
I'm not sure I understand what information you need, even though your request is clear and specific. Or perhaps, I don't know enough to create a simpler example based on my actual problem, which is a complex piece of code. In my inexpert opinion, the purpose is to keep typeshed as the SSOT for type information, which requires accuracy for the sake of accuracy. Excerpt from astToolFactory def _get_astModule_astStub() -> ast.Module:
ImaSearchContext: typeshed_client.SearchContext = typeshed_client.get_search_context(typeshed=pathRoot_typeshed)
return parsePathFilename2astModule(raiseIfNone(typeshed_client.get_stub_file("ast", search_context=ImaSearchContext)))
...
dictionaryClassDef: dict[str, ast.ClassDef] = makeDictionaryClassDef(_get_astModule_astStub())
...
def get_type_ast_expr(dddataframeee: pandas.DataFrame) -> tuple[pandas.Series, pandas.Series]:
attribute: str = cast("str", dddataframeee["attribute"])
if attribute == 'n':
# TODO remove when `ast.Num` is updated
attribute = 'value'
getAnnotation = NodeTourist[ast.AnnAssign, ast.expr](
findThis = Be.AnnAssign.targetIs(IfThis.isNameIdentifier(attribute))
, doThat = Then.extractIt(DOT.annotation))
ClassDefIdentifier: str = cast("str", dddataframeee["ClassDefIdentifier"])
if ClassDefIdentifier == 'NameConstant':
ClassDefIdentifier = 'Constant'
type_ast_expr: ast.expr = raiseIfNone(getAnnotation.captureLastMatch(dictionaryClassDef[ClassDefIdentifier]))
NodeChanger(findThis=Be.Subscript.valueIs(IfThis.isNameIdentifier("Literal")), doThat=Then.replaceWith(Make.Name("bool"))
).visit(type_ast_expr)
dddataframeee["type_ast_expr"] = NodeChanger[ast.Name, ast.expr](
findThis=lambda node: Be.Name(node) and isinstance(eval(node.id), type) and issubclass(eval(node.id), ast.AST) # noqa: S307
, doThat=lambda node: Make.Attribute(Make.Name("ast"), eval(node.id).__name__) # noqa: S307
).visit(type_ast_expr)
dddataframeee["attributeType"] = ast.unparse(cast("ast.AST", dddataframeee["type_ast_expr"]))
return dddataframeee["type_ast_expr"], dddataframeee["attributeType"]For each subclass ( Oh, wait, was. I need to update that code. I will continue trying to think of a way to create a simpler example. Sorry. |
|
I've opened #14397, which is an alternative that aligns the stubs closer with the implementation by adding |
Fantastic! I didn't have the knowledge to see that option, and it seems like a far better solution. |