Repository navigation
Pyupgrade - #4741
Pyupgrade#4741
Conversation
| except OSError: | ||
| e = sys.exc_info()[1] | ||
| if e.errno == errno.ENOENT: |
There was a problem hiding this comment.
Maybe this could be simplified like so?
| except OSError: | |
| e = sys.exc_info()[1] | |
| if e.errno == errno.ENOENT: | |
| except OSError as e: | |
| if e.errno == errno.ENOENT: |
There was a problem hiding this comment.
definitely but I didn't want to actually change things in here manually. This PR is merely for the automation part
There was a problem hiding this comment.
even better, you can catch FileNotFoundError separately
| with suppress(Exception): | ||
| await comm.close() | ||
| raise IOError( | ||
| raise OSError( |
There was a problem hiding this comment.
Did we intend for these exceptions to be changed?
|
There are a few places where this tooling seems to be changing the exceptions we are raising. This seems a little unexpected and may have surprising consequences. Is there a way to disable that? |
There was a problem hiding this comment.
This looks really useful.
I especially enjoyed seeing the "%s" % var and "{}".format(var) calls being upgraded to use f-strings. Although it is interesting to note that not everything is being switched to f-string. Some things with more complex expressions seem to be stopping at .format(). Perhaps we want to refactor those so that everything is f-string?
Seeing the exceptions being changed was a little surprising. But it's my understanding that these days OSError is IOError is EnvironmentError is socket.error so switching them all out to be OSError makes sense.
The only other change I really noticed was set([1, 2, 3]) being replaced with {1, 2, 3}.
My one concern about this change is adding it to the CI via the pre-comimt check. Expecting every contributor to always write 100% modern Python just adds a little more friction. We experience this today with folks not running Black. Unless everyone uses pre-commit (which is opt-in) they will have to also run pyupgrade themselves, or maintainers will need to push another commit for them. This is only a small concern, but wanted to raise it. Perhaps we should just run this periodically ourselves instead of making it a hard requirement?
Yes, I guess the magic is limited to some extend. Maybe this is only a bug, or the AST is, for whatever reason, too complex, idk. If it boils down to a few remaining
@jakirkham The exceptions we are raising are indeed altered but according to the python docs these are identical to a I think it is not possible to disable this https://github.com/asottile/pyupgrade#oserror-aliases
The referred subclasses are listed in https://docs.python.org/3/library/exceptions.html#os-exceptions and include things like # Python 3.8.8
In [2]: OSError is IOError
Out[2]: True
In [3]: IOError is OSError
Out[3]: True
In [4]: IOError == OSError
Out[4]: True
In [5]: OSError == IOError
Out[5]: True
In [1]: ConnectionError == OSError
Out[1]: False
In [2]: ConnectionError is OSError
Out[2]: False
That is a valid concern but I figured this was already the case for black. IMHO it is the same for isort and flake8. While flake8 does not auto-format things, I personally am not able to simply write flake8 compatible code, if only for too long comments, etc. |
|
If this is accepted, I'll recreate the last commit which applies pyupgrade. No need for merging. |
|
This seems fine to me |
jrbourbeau
left a comment
There was a problem hiding this comment.
Yeah, agreed that this is fine by me. At some point I'll want to update our contributing docs to say "use pre-commit for code styling" instead of recommending it as an optional nice-to-have
| raise ValueError( | ||
| "Unexpected keyword arguments: {}".format(str(sorted(kwargs))) | ||
| ) | ||
| raise ValueError(f"Unexpected keyword arguments: {str(sorted(kwargs))}") |
There was a problem hiding this comment.
| raise ValueError(f"Unexpected keyword arguments: {str(sorted(kwargs))}") | |
| raise ValueError(f"Unexpected keyword arguments: {sorted(kwargs)}") |
| "Occupancy -- total time: {} wall time: {}".format( | ||
| format_time(total), | ||
| format_time(total / self.scheduler.total_nthreads), |
There was a problem hiding this comment.
| "Occupancy -- total time: {} wall time: {}".format( | |
| format_time(total), | |
| format_time(total / self.scheduler.total_nthreads), | |
| f"Occupancy -- total time: {format_time(total)} " | |
| f"wall time: {format_time(total / self.scheduler.total_nthreads)}" |
| " " | ||
| + bcolors.FAIL | ||
| + "Retrying... (attempt {n}/{total})".format(n=retries, total=3) | ||
| + f"Retrying... (attempt {retries}/{3})" |
There was a problem hiding this comment.
| + f"Retrying... (attempt {retries}/{3})" | |
| + f"Retrying... (attempt {retries}/3)" |
| "[ {label} ] : ".format(label=cmd_dict["label"]) | ||
| + bcolors.FAIL | ||
| + "{output}".format(output=line) | ||
| + f"{line}" |
There was a problem hiding this comment.
| + f"{line}" | |
| + line |
|
@crusaderky I'll update the PR but it is probably not worth it to review individual lines. This is automatically changed. I'll rebase and we can have a look |
| # Optionally re-direct stdout and stderr to a logfile | ||
| if logdir is not None: | ||
| cmd = "mkdir -p {logdir} && ".format(logdir=logdir) + cmd | ||
| cmd = f"mkdir -p {logdir} && " + cmd |
There was a problem hiding this comment.
| cmd = f"mkdir -p {logdir} && " + cmd | |
| cmd = f"mkdir -p {logdir} && {cmd}" |
| + "scheduler {addr}:{port}".format(addr=addr, port=port) | ||
| + bcolors.ENDC | ||
| ) | ||
| label = bcolors.BOLD + f"scheduler {addr}:{port}" + bcolors.ENDC |
There was a problem hiding this comment.
| label = bcolors.BOLD + f"scheduler {addr}:{port}" + bcolors.ENDC | |
| label = f"{bcolors.BOLD}scheduler {addr}:{port}{bcolors.ENDC}" |
| # Optionally redirect stdout and stderr to a logfile | ||
| if logdir is not None: | ||
| cmd = "mkdir -p {logdir} && ".format(logdir=logdir) + cmd | ||
| cmd = f"mkdir -p {logdir} && " + cmd |
There was a problem hiding this comment.
| cmd = f"mkdir -p {logdir} && " + cmd | |
| cmd = f"mkdir -p {logdir} && {cmd}" |
|
|
||
| def read(self, deserializers=None): | ||
| raise EnvironmentError | ||
| raise OSError |
There was a problem hiding this comment.
| raise OSError | |
| raise OSError() |
|
|
||
| def write(self, msg, serializers=None, on_error=None): | ||
| raise EnvironmentError | ||
| raise OSError |
There was a problem hiding this comment.
| raise OSError | |
| raise OSError() |
There was a problem hiding this comment.
out of curiosity, is this a style note or is there a functional difference?
There was a problem hiding this comment.
it's only a style note. except OSError as e will set e to an instance to OSError in both cases.
|
|
||
| def read(self, deserializers=None): | ||
| raise EnvironmentError | ||
| raise OSError |
There was a problem hiding this comment.
| raise OSError | |
| raise OSError() |
|
|
||
| def write(self, msg, serializers=None, on_error=None): | ||
| raise EnvironmentError | ||
| raise OSError |
There was a problem hiding this comment.
| raise OSError | |
| raise OSError() |
| if result2 == expected2: | ||
| return | ||
| raise Exception("Expected: {}; got: {}".format(str(expected2), str(result2))) | ||
| raise Exception(f"Expected: {str(expected2)}; got: {str(result2)}") |
There was a problem hiding this comment.
| raise Exception(f"Expected: {str(expected2)}; got: {str(result2)}") | |
| raise Exception(f"Expected: {expected2}; got: {result2}") |
|
|
||
| def read(self, deserializers=None): | ||
| raise EnvironmentError | ||
| raise OSError |
There was a problem hiding this comment.
| raise OSError | |
| raise OSError() |
|
|
||
| def write(self, msg, serializers=None, on_error=None): | ||
| raise EnvironmentError | ||
| raise OSError |
There was a problem hiding this comment.
| raise OSError | |
| raise OSError() |
|
I just rebased and updated. We can either
I suggest to do further refinements in a follow up PR |
I think pyupgrade in the pre-commit is a bit overkill |
| @@ -84,7 +84,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env= | |||
| stderr=(subprocess.PIPE if hide_stderr else None), | |||
There was a problem hiding this comment.
This file is auto-generated by versioneer and IMHO should be left untouched and excluded from pyupgrade
There was a problem hiding this comment.
I'll revert. FWIW we already formatted the file in a black formatting step ~2years ago
| # add more workers if more than 60% of memory is used | ||
| limit = sum([ws._memory_limit for ws in parent._workers_dv.values()]) | ||
| used = sum([ws._nbytes for ws in parent._workers_dv.values()]) | ||
| limit = sum(ws._memory_limit for ws in parent._workers_dv.values()) |
There was a problem hiding this comment.
This seems to cause some problems with cythonization
There was a problem hiding this comment.
Looks like cython doesn't like the generator expressions
|
I reverted the used pyupgrade version to 2.13.0 since this version worked before. The reformatting of I also removed it from the pre-commit config, i.e. it would not be enforced now. |
|
Based off a brief discussion on our dask maint weekly, we think this will not cause major merge conflicts so I'll go ahead and merge this now. pyupgrade will not be added to pre-commit in this PR |
|
Thanks @fjetter |
I personally love automated formatting and I discovered this gem in the pandas pre-commit hooks. While deprecations are less common in py3 (opposed to the py2->py3 migration) they do happen. Often this boils down to slightly less verbose syntax, other times exception classes or functions are renamed, etc.
Some changes are merely for style while others, e.g. around exception classes, imho clarify things better, e.g. what's the distinction between
OSError,IOError,EnvironmentError,socket.error? Nothing... since py3.3Is this kind of thing something people would feel comfortable with or is this too much?