Skip to content

Conversation

@hunterhogan
Copy link
Contributor

Refine the type annotation for _field_types to a more specific union of types, enhancing type safety and clarity in the codebase.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2025

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra
Copy link
Member

This is incorrect, _field_types doesn't hold any of those types.

@hunterhogan
Copy link
Contributor Author

hunterhogan commented Jun 8, 2025 via email

@hunterhogan
Copy link
Contributor Author

Issue created #14245

This is incorrect, _field_types doesn't hold any of those types.

"those types":

_ConstantValue | AST | list[AST] | list[str]

True, the annotation is not precise, and it may be incorrect, but _field_types certainly holds at least one of those types: list[str].

>>> ast.Global._field_types
{'names': list[str]}
>>> ast.Nonlocal._field_types
{'names': list[str]}
>>> ast.MatchClass._field_types['kwd_attrs']
list[str]

I sincerely don't know how to evaluate your claim that _field_types doesn't hold _ConstantValue. I think _ConstantValue is a "UnionType", but even if it is a "UnionType," I have not been able to decipher the implications of that. So, I don't know for sure whether or not _ConstantValue in this context is malformed syntax. I thought it was valid syntax, but your challenge that none of the four types are correct makes me think it could be malformed.

I omitted some types from the annotation because I believed they were represented by _ConstantValue. As you know, the types in _ConstantValue are str | bytes | bool | int | float | complex | None | EllipsisType. Of those, excluding ast.Constant and its subclasses:

  • ✔️ str is held by _field_types,
  • bytes is not held by _field_types,
  • ❌ I don't know for sure that Literal[True, False] is the same as bool, so perhaps it is not held by _field_types,
  • ✔️ int is held by _field_types,
  • float is not held by _field_types,
  • complex is not held by _field_types,
  • ✔️ None is held by _field_types,
  • EllipsisType or Ellipsis or ellipsis is not held by _field_types.

@JelleZijlstra
Copy link
Member

The dictionary contains the value list[str], which is not an instance of the type list[str].

The correct annotation for this field is arguably TypeForm from the draft PEP 747, though it could be something a little narrower since the actual set of types in the ast module is a bit smaller than all possible type forms.

@hunterhogan
Copy link
Contributor Author

The dictionary contains the value list[str], which is not an instance of the type list[str].

I did not see that coming.

I just reread the ClassVar documentation. Despite now knowing the above is true, I can't decipher the documentation.

Thank you for the explanation.

@hunterhogan hunterhogan closed this Jun 9, 2025
@JelleZijlstra
Copy link
Member

This is nothing to do with ClassVar, it's how all type annotations work.

@hunterhogan
Copy link
Contributor Author

This is nothing to do with ClassVar, it's how all type annotations work.

Hmm. I suspect I have a significant defect in my mental model of type annotations.

Oh no, I hope my mental model isn't flawed because I don't understand OOP despite 30+ years of exposure to OOP. 😨

@hunterhogan hunterhogan deleted the _field_typesClassVar branch June 9, 2025 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants