Skip to content

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

Merged
sergey-tomin merged 1 commit into
devfrom
magnetic-lattice-type-hints-improvements
Jul 3, 2026
Merged

Update type hints to use union syntax for None in MagneticLattice class#312
sergey-tomin merged 1 commit into
devfrom
magnetic-lattice-type-hints-improvements

Conversation

@st-walker

Copy link
Copy Markdown
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

Copilot AI left a comment

Copy link
Copy Markdown

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

Copilot AI Jan 28, 2026

Copy link

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

Copilot AI Jan 28, 2026

Copy link

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.
@sergey-tomin sergey-tomin merged commit 1660153 into dev Jul 3, 2026
6 checks passed
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.

3 participants