-
Notifications
You must be signed in to change notification settings - Fork 94
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
Conversation
def __hash__(self) -> int: | ||
return hash(self.value) |
There was a problem hiding this comment.
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)
cylc/flow/cycling/loader.py
Outdated
@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]: |
There was a problem hiding this comment.
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
There was a problem hiding this 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)
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 |
(Not merged yet) |
It's generally a sensible rule, so I'd rather not ignore it. The code looks OK as suggested in my previous comment, no? |
Ok, if you prefer |
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 👍 |
Waiting on #4217 which fixes the manylinux tests |
@property | ||
@abstractmethod | ||
def TYPE(self): | ||
return self._TYPE | ||
def TYPE(self) -> str: | ||
"""Cycling type of this point.""" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...).
There was a problem hiding this comment.
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_KEY
s to ints. This is how they are used (on master)
cylc-flow/cylc/flow/cycling/__init__.py
Lines 110 to 118 in 87e22ab
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) |
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 |
I can't replicate it, however, the actions artefact captures the issue, the subprocess pool encountered a |
MacOS failure seems unrelated, marking as ready for review |
@@ -71,7 +71,6 @@ jobs: | |||
|
|||
- name: style | |||
run: | | |||
pycodestyle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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). 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
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
CONTRIBUTING.md
and added my name as a Code Contributor.setup.py
andconda-environment.yml
.