-
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
flake8: simplify #4271
flake8: simplify #4271
Conversation
with Conf('platform groups'): # noqa: SIM117 (keep same format) | ||
with Conf('<group>'): |
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 combine these two with
statements into a single one, however, since all of the others in this file are written individually this would break the pattern somewhat.
@@ -1538,7 +1539,7 @@ def upgrade_graph_section(cfg: Dict[str, Any], descr: str) -> None: | |||
"""Upgrade Cylc 7 `[scheduling][dependencies][X]graph` format to | |||
`[scheduling][graph]X`.""" | |||
# Parsec upgrader cannot do this type of move | |||
try: | |||
with contextlib.suppress(KeyError): |
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.
with suppress(Something):
something()
Is generally cleaner and more readable than:
try:
something()
except Something:
pass
I'm not so fussed, if this causes us undue pain we can always disable the rule, however, it does have a potential advantage from the perspective of code coverage so thats nice.
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 quite like the with suppress...
version.
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.
One thing to bear in mind is with suppress
is slower, so we might want to avoid it sometimes
from contextlib import suppress
def f():
try:
int('a')
except ValueError:
pass
def g():
with suppress(ValueError):
int('a')
In [5]: %timeit f()
976 ns ± 46 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [6]: %timeit g()
1.63 µs ± 32.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
if not self.cfg['scheduler']['allow implicit tasks']: | ||
raise WorkflowConfigError( | ||
f"{err_msg}\n\n" | ||
"To allow implicit tasks, use " | ||
"'flow.cylc[scheduler]allow implicit tasks'") | ||
else: | ||
LOG.debug(err_msg) |
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.
flake8-simplify wants you to handle the error cases first, so this:
if error_cond:
raise Exception()
something()
Rather than this:
if not error_cond:
something()
else:
raise Exception()
This is generally a good idea, however, I think for pattern-matching type problems where you have a list of conditions and handlers it's a good pattern to have a else: raise
catchall at the end:
if cond_a:
something('a')
elif cond_b:
something('b')
...
elif cond_z:
something('z')
else:
raise Exception()
So I've left these example in with noqa flags.
if ',' in exclusions: | ||
if (not exclusions.strip().startswith('(') or not |
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.
Needlessly nested if's
@@ -417,7 +423,7 @@ def __hash__(self) -> int: | |||
pass | |||
|
|||
|
|||
class ExclusionBase(object, metaclass=ABCMeta): | |||
class ExclusionBase(metaclass=ABCMeta): |
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.
Python2 class.
sys.exit(1) | ||
except IOError: | ||
sys.exit("Workflow schd program exited") | ||
with open(logfname, 'r') as logfile: |
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.
with open()
is the better pattern.
Note that this:
with open() as h:
h.write()
Is not equivalent to this:
h = open()
h.write()
h.close()
As the with open
brings extra error handling to the table.
return any( | ||
message == value | ||
for outputs_str, in res | ||
for value in json.loads(outputs_str).values() | ||
) |
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.
any
is faster and more readable.
@@ -187,7 +192,7 @@ def get_data_elements(flow, nat_ids, element_type): | |||
] | |||
|
|||
|
|||
class BaseResolvers: | |||
class BaseResolvers: # noqa: SIM119 (no real gain + mutable default) |
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.
flake8-simplify is keen on dataclasses.
I.E. this:
from dataclasses import dataclass
@dataclass
class Foo:
a: str
b: str
Rather than this:
class Foo:
def __init__(self, a: str, b: str):
self.a = a
self.b = b
Dataclasses have additional benefits because they add other methods like __str__
and __eq__
which saves a lot of boilerplate of thekind we are often to lazy to write.
Dataclasses are generally a good idea, however, a lot of our classes (like this one) don't fit the pattern so well.
Nice to be reminded that we could use a dataclass, just stick a noqa
in if not appropriate.
d61609d
to
2b21d41
Compare
2b21d41
to
44802cb
Compare
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.
👍 Quite a big change but just straightforward switch to other valid coding constructs, so one review will do on this.
|
(we can use the flake8 |
More meeting code fiddling, closes #4239
List of simplify codes here: https://github.com/MartinThoma/flake8-simplify#SIM106
Used a few
noqa
flags where appropriate, there are a few things in the code that are hard to refactor, usually patterns which should be avoided but which aren't worth the pain of totally overhauling at this stage.Suggest adding
?=1
to the diff URL to hide the whitespace changes.Requirements check-list
CONTRIBUTING.md
and added my name as a Code Contributor.setup.py
andconda-environment.yml
.