Skip to content

Update type hints to use union syntax for None in MagneticLattice class#312

Open
st-walker wants to merge 1 commit intodevfrom
magnetic-lattice-type-hints-improvements
Open

Update type hints to use union syntax for None in MagneticLattice class#312
st-walker wants to merge 1 commit intodevfrom
magnetic-lattice-type-hints-improvements

Conversation

@st-walker
Copy link
Collaborator

Get rid of annoying complains because type hints for MagneticLattice not done properly

Get rid of annoying complains because type hints for MagneticLattice not done properly
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 start and stop parameters in __init__, transfer_maps, and print_sequence methods to use E | None syntax

💡 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):
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines +344 to +345
def transfer_maps(self, energy, output_at_each_step: bool = False, start: E | None = None,
stop: E | None = None):
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MagneticLattice.transfer_maps returns tuple of size 3 and tuple of size 4.

Copilot uses AI. Check for mistakes.
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