Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type annotations to ClusterSequence #87

Merged
merged 4 commits into from
Oct 19, 2022
Merged

Conversation

ssrothman
Copy link
Contributor

This PR adds return type annotations to the ClusterSequence class, allowing code analysis tools such as pylance to understand fastjet methods. The previous behavior was that pylance read the abstract method definitions and concluded that the methods allows raised an error. This marked all code after a fastjet call as unreachable.

@jpivarski
Copy link
Member

On the test failure:

Error: getCacheEntry failed: GitHub Actions is temporarily unavailable. Please visit https://www.githubstatus.com/ for the status of our services.

On the PR itself: I think this is a valuable thing, if it gets past a blocker. fastjet is more easily typeable than most Awkward Array-based projects because some of these methods can only return ak.Array, not ak.Record or a scalar. In more general applications, whether a given slice returns an array or a scalar (ak.Record is a kind of scalar) depends on the type of the arguments, which is not a distinction in Python types. (As far as Python is concerned, it's a difference in values, not types.)

Actually, there might be a few of these methods that, when they accept a single event (ak.Array of Momentum4D) as input, return a single number (scalar). Those cannot be annotated with -> ak.Array because whether they return an ak.Array or an int/float depends on whether the input ak.Array has Awkward type # * Momentum4D (single event) or # * var * Momentum4D (multiple events), which have the same Python type. @aryan26roy should scan the changes proposed in this PR to be sure that you're only annotating the cases that we know will always return ak.Array.

Copy link
Collaborator

@aryan26roy aryan26roy left a comment

Choose a reason for hiding this comment

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

Sorry for the late reply, I have been busy with a lot of stuff in the past 2 months. I have commented on all the methods that have been marked as returning an Awkward Array but can return a single value (bool, int or float) if the input is a single event type.

@@ -295,7 +295,7 @@ def constituents(self, min_pt=0):
"""
raise AssertionError()

def exclusive_dmerge(self, njets=10):
def exclusive_dmerge(self, njets=10) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can return a single number (single event).

@@ -306,7 +306,7 @@ def exclusive_dmerge(self, njets=10):
"""
raise AssertionError()

def exclusive_dmerge_max(self, njets=10):
def exclusive_dmerge_max(self, njets=10) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same problem.

@@ -317,7 +317,7 @@ def exclusive_dmerge_max(self, njets=10):
"""
raise AssertionError()

def exclusive_ymerge_max(self, njets=10):
def exclusive_ymerge_max(self, njets=10) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here as well.

@@ -328,7 +328,7 @@ def exclusive_ymerge_max(self, njets=10):
"""
raise AssertionError()

def exclusive_ymerge(self, njets=10):
def exclusive_ymerge(self, njets=10) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This too.

@@ -339,7 +339,7 @@ def exclusive_ymerge(self, njets=10):
"""
raise AssertionError()

def Q(self):
def Q(self) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This too.

@@ -422,7 +422,7 @@ def n_exclusive_subjets(self, data, dcut=0):
"""
raise AssertionError()

def has_parents(self, data):
def has_parents(self, data) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can return a single Bool.

@@ -433,7 +433,7 @@ def has_parents(self, data):
"""
raise AssertionError()

def has_child(self, data):
def has_child(self, data) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can return a single Bool.

@@ -444,7 +444,7 @@ def has_child(self, data):
"""
raise AssertionError()

def jet_scale_for_algorithm(self, data):
def jet_scale_for_algorithm(self, data) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can return a single number.

@@ -466,7 +466,7 @@ def unique_history_order(self):
"""
raise AssertionError()

def n_particles(self):
def n_particles(self) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This too.

@@ -477,7 +477,7 @@ def n_particles(self):
"""
raise AssertionError()

def n_exclusive_jets(self, dcut=0):
def n_exclusive_jets(self, dcut=0) -> ak.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This too.

@jmduarte jmduarte changed the title Add return type annotations to ClusterSequence Add type annotations to ClusterSequence Oct 18, 2022
@lgray lgray merged commit a6a4726 into scikit-hep:main Oct 19, 2022
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.

5 participants