Update type hints to use union syntax for None in MagneticLattice class#312
Update type hints to use union syntax for None in MagneticLattice class#312
Conversation
Get rid of annoying complains because type hints for MagneticLattice not done properly
There was a problem hiding this comment.
Pull request overview
This PR updates type hints in the MagneticLattice class to use the modern union syntax (X | Y) instead of the older Optional[X] or Union[X, None] syntax. The changes aim to resolve type hint complaints in development tools like PyCharm.
Changes:
- Updated type hints for
startandstopparameters in__init__,transfer_maps, andprint_sequencemethods to useE | Nonesyntax
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| """ | ||
|
|
||
| def __init__(self, sequence, start: E = None, stop: E = None, method=None): | ||
| def __init__(self, sequence, start: E | None = None, stop: E | None = None, method=None): |
There was a problem hiding this comment.
The union syntax (PEP 604) E | None requires Python 3.10+. Since setup.py specifies python_requires=">=3.9", this change will break compatibility with Python 3.9 unless you add from __future__ import annotations at the top of this file (before other imports).
The future import enables postponed evaluation of annotations, allowing the | syntax to work on Python 3.9. This is already done in several other files in the codebase (ocelot/cpbd/track.py, ocelot/gui/layout_3d.py, ocelot/utils/acc_utils.py, and ocelot/utils/bba.py).
Either add the future import or revert to using Optional[E] syntax (which requires importing Optional from typing).
| def transfer_maps(self, energy, output_at_each_step: bool = False, start: E | None = None, | ||
| stop: E | None = None): |
There was a problem hiding this comment.
MagneticLattice.transfer_maps returns tuple of size 3 and tuple of size 4.
Get rid of annoying complains because type hints for MagneticLattice not done properly