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

Tidy & add type annotations #4213

Merged
merged 8 commits into from
May 26, 2021
Merged

Tidy & add type annotations #4213

merged 8 commits into from
May 26, 2021

Conversation

MetRonnie
Copy link
Member

This is a small change with no associated Issue.

While tackling #3743, I started adding type annotations to help me understand the existing code. Then somehow ended up tidying some of the methods in the cyclers classes

Requirements check-list

  • I have read CONTRIBUTING.md and added my name as a Code Contributor.
  • Contains logically grouped changes (else tidy your branch by rebase).
  • Does not contain off-topic changes (use other PRs for other changes).
  • Applied any dependency changes to both setup.py and
    conda-environment.yml.
  • Already covered by existing tests.
  • No change log entry required (invisible to users).
  • No documentation update required.

@MetRonnie MetRonnie added this to the cylc-8.0.0 milestone May 14, 2021
@MetRonnie MetRonnie requested review from kinow and wxtim May 14, 2021 10:46
@MetRonnie MetRonnie self-assigned this May 14, 2021
Comment on lines +295 to +296
def __hash__(self) -> int:
return hash(self.value)
Copy link
Member Author

@MetRonnie MetRonnie May 14, 2021

Choose a reason for hiding this comment

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

I don't think __hash__ was being implemented in IntegerInterval or ISO8601Interval before this, so two ostensibly equal intervals would have had different hashes (i.e. this change replaces per-instance hashes with per-value hashes)

Comment on lines 66 to 77
@overload
def get_point(value: str, cycling_type: Optional[str] = None) -> PointBase: ...


@overload
def get_point(value: None, cycling_type: Optional[str] = None) -> None: ...


def get_point(
value: str, cycling_type: Optional[str] = None
value: Optional[str], cycling_type: Optional[str] = None
) -> Optional[PointBase]:
Copy link
Member Author

@MetRonnie MetRonnie May 14, 2021

Choose a reason for hiding this comment

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

This is how you type annotate a function whose return value depends on an input arg

Copy link
Member

@kinow kinow left a comment

Choose a reason for hiding this comment

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

pycodestyle not happy?

./cylc/flow/cycling/loader.py:67:1: E704 multiple statements on one line (def)
./cylc/flow/cycling/loader.py:71:1: E704 multiple statements on one line (def)
./cylc/flow/cycling/loader.py:146:1: E704 multiple statements on one line (def)
./cylc/flow/cycling/loader.py:152:1: E704 multiple statements on one line (def)

@MetRonnie
Copy link
Member Author

MetRonnie commented May 14, 2021

https://www.flake8rules.com/rules/E704.html

I'm proposing we add this rule to the ignored list. What do you think @hjoliver ?

Also, we're running pycodestyle and flake8 for some reason - we only need flake8 as it includes pycodestyle

@MetRonnie MetRonnie marked this pull request as draft May 14, 2021 12:00
@hjoliver
Copy link
Member

Also, we're running pycodestyle and flake8 for some reason - we only need flake8 as it includes pycodestyle

#3999 (comment)

(Not merged yet)

@hjoliver
Copy link
Member

https://www.flake8rules.com/rules/E704.html

I'm proposing we add this rule to the ignored list. What do you think @hjoliver ?

It's generally a sensible rule, so I'd rather not ignore it. The code looks OK as suggested in my previous comment, no?

@MetRonnie
Copy link
Member Author

Ok, if you prefer

@MetRonnie
Copy link
Member Author

It would be good to remove pycodestyle sooner, as at the moment, flake8 is ignoring more rules than pycodestyle alone

@hjoliver
Copy link
Member

It would be good to remove pycodestyle sooner, as at the moment, flake8 is ignoring more rules than pycodestyle alone

OK, feel free to remove it here 👍

@MetRonnie
Copy link
Member Author

Waiting on #4217 which fixes the manylinux tests

@MetRonnie MetRonnie closed this May 18, 2021
@MetRonnie MetRonnie reopened this May 18, 2021
Comment on lines +62 to +65
@property
@abstractmethod
def TYPE(self):
return self._TYPE
def TYPE(self) -> str:
"""Cycling type of this point."""
Copy link
Member Author

Choose a reason for hiding this comment

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

This is how you do an abstract property in Python 3.3+
https://stackoverflow.com/a/48710068/3217306

CYCLER_TYPE_SORT_KEY_INTEGER = "a"
CYCLER_TYPE_SORT_KEY_INTEGER = 0
Copy link
Member Author

@MetRonnie MetRonnie May 19, 2021

Choose a reason for hiding this comment

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

Sorting by integer seems more natural than by string. Not that I particularly understand why we would ever compare Integer and ISO8601 cycling things with each other

Copy link
Member

@hjoliver hjoliver May 23, 2021

Choose a reason for hiding this comment

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

We could conceivably allow other cycling types in the future (e.g. to cycle over a list of named datasets). They could all be represented as strings, so long as integer cycles are also represented as strings (but compared as integers). The Point and Interval base class docstrings in cylc/flow/cycling/__init__.py say that derived classes should be "string based".

However, (as your comment refers to?) this TYPE_SORT_KEY seems to be used as a proxy for instance comparison when comparing points of different cycling types ... and I don't understand why we'd want to do that either. But if we did do it, this change would break the comp function? [update: I see you changed the ISO8601 type_sort_key to an int too ... now I'm even more confused!] Any thoughts @oliver-sanders ? (git blame points to Ben on this, but I think it predates your involvement...).

Copy link
Member Author

@MetRonnie MetRonnie May 24, 2021

Choose a reason for hiding this comment

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

Yes, I changed both integer's & iso8601's TYPE_SORT_KEYs to ints. This is how they are used (on master)

def __cmp__(self, other):
# Compare to other point.
if other is None:
return -1
if self.TYPE != other.TYPE:
return cmp(self.TYPE_SORT_KEY, other.TYPE_SORT_KEY)
if self.value == other.value:
return 0
return self.cmp_(other)

@MetRonnie
Copy link
Member Author

Hmm, MacOS functional test failing; looks like it's happened on a previous run too. @oliver-sanders can I ask you to check this branch out and run tests/f/restart/28-execution-timeout.t?

@oliver-sanders
Copy link
Member

I can't replicate it, however, the actions artefact captures the issue, the subprocess pool encountered a PermissionError whilst trying to kill a process group. On MacOS this can happen for zombie processes, MacOS fails loudly, I think Linux might fail silently masking the bug? Running a test.

@MetRonnie MetRonnie marked this pull request as ready for review May 20, 2021 16:51
@MetRonnie
Copy link
Member Author

MacOS failure seems unrelated, marking as ready for review

@MetRonnie MetRonnie requested a review from kinow May 21, 2021 14:36
@@ -71,7 +71,6 @@ jobs:

- name: style
run: |
pycodestyle
Copy link
Member

Choose a reason for hiding this comment

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

👍

cylc/flow/config.py Outdated Show resolved Hide resolved
cylc/flow/config.py Show resolved Hide resolved
Copy link
Member

@kinow kinow left a comment

Choose a reason for hiding this comment

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

Looks good to me. Used PyCharm integration to run MyPy, it found 23 issues, but none related to the changes here (and could well be false-positives). 👍

cylc/flow/workflow_db_mgr.py Show resolved Hide resolved
@MetRonnie MetRonnie mentioned this pull request May 25, 2021
7 tasks
Copy link
Member

@hjoliver hjoliver left a comment

Choose a reason for hiding this comment

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

👍

@hjoliver hjoliver merged commit 6d2d55c into cylc:master May 26, 2021
@MetRonnie MetRonnie deleted the dev branch May 26, 2021 08:33
@dpmatthews dpmatthews modified the milestones: cylc-8.0.0, cylc-8.0b2 Jun 14, 2021
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