-
-
Notifications
You must be signed in to change notification settings - Fork 23
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 custom rewrites, using e.g. libcst
#22
Comments
except:
with except BaseException:
libcst
@Zac-HD Are these rewrites always run or only with --refactor? |
Update: it wasn't as fast as I'd hoped, so all these rewrites are only run in |
This comment was marked as outdated.
This comment was marked as outdated.
Another pattern I've noticed occasionally is that functions with parametrized-generic-type-annoted args are much easier to read with one-param-per-line. I'm still unsure of the exact cutoff for forcing this, but something like:
There's probably a nice way of aggregating these factors as a score so that it works nicely in mixed-annotation-depth situations, but I'm happy to look at that when developing a prototype. |
This comment was marked as outdated.
This comment was marked as outdated.
Go for it, and no, it's not 🙂 |
I think the |
@Zac-HD I guess this it the nested Union example: and this is the nested Literal example: As these are the 2 in the example that is not covered yet? Correct me if I am wrong, then I will start playing with it. |
Yep, that's it 😊 |
I think the last task is not ready till 2023? |
Correct, though if you really wanted to you could implement it early conditional on I've also pulled all the other ideas in comments up into the checklist. |
- Replace builtins with literal values - Replace empty list constructions with [] Completes a couple tasks in Zac-HD#22.
request: remove empty type-checking blockif running flake8 with if TYPE_CHECKING:
pass this can clearly be removed without any side effects. additional luxury request: automatically add imports for type-checking types (possibly in a type-checking block).Doing it for all imports seems dangerous, but a whitelist for the types in |
I'd be happy to remove any if-statement where the test is a name and the body is Luxury request: https://libcst.readthedocs.io/en/latest/codemods.html#library-of-transforms provides some useful tools for this. Modifying imports shouldn't take a full second pass though, that can be a one-shot modification to the final tree. I'd be happy to accept a PR for this, though it's pretty low priority. |
Is there any reason that types in Union/Literal/type annotations only put foo: set[cst.Return | cst.Yield] = ...
bar: list[cst.Yield | cst.Return] = ... which is quite irritating. I'm pretty sure there's lots of instances of it in flake8-trio. A related rewrite is literal sets: a = set(2, 1) and duplicates in either: bar: set[int | float | int] = set(1, 1) |
Why not sort? => I didn't want to break deliberate orderings, and can imagine this being pretty annoying. (otherwise, yeah, there's a lot we could do there) Set-literal rewrites would be great, but duplicates are probably better as a lint rule since there's a good chance one element is a typo! |
might I interest you in supporting |
re pie802: prefer simple any/all - this isn't fully safe since any/all will shortcut but |
@charliermarsh I'm not sure whether you've been following this issue or https://github.com/Zac-HD/shed/blob/master/CODEMODS.md, but I'd be very very happy if I could close it as " |
@Zac-HD -- Oh cool! I think I'd need to go through and annotate which of these are supported by Ruff. Would that be helpful? Is it mostly the unchecked items that matter? |
Oh right, I forgot this issue existed.
The checked ones will either be listed in CODEMODS.md, or was removed in #105 because they were covered by ruff, so it's only the unchecked ones that have unknown ruff support. |
Unchecked items here are unsupported by |
We support this as SIM103 with a fix, though we only catch one of the two cases in the PIE docs. I made an issue here: astral-sh/ruff#10402.
We support this as SIM101 with a fix.
We support this as C417, with a fix. We support this exact rule, with a fix. We support this exact rule, with a fix. The rest we either don't do, or we do some but not all... We don't catch the list comprehension in |
assert a and b
into two separate assertions, for easier debugging. Preserve comments.libcst
#22 (comment) - this should be in a separate PR so I can evaluate the heuristic on some real large code corporalambda x: whatever(x)
to justwhatever
.[]
, plus{}
,()
.sorted(list(...))
, remove the inner call tolist()
(etc; see flake8-comprehensions for more)"...".join(list(...))
, remove the call tolist()
(ortuple()
)all()
,dict()
,sum()
,min()
, andmax()
raise NotImplemented
should beraise NotImplementedError
assert False
withraise AssertionError
, handling the optional argUnion
andLiteral
contents so thatNone
is lastUnion
orLiteral
instances|
so that None is lastconvert_none_cmp()
to handleTrue
andFalse
too, ala CompareSingletonPrimitivesByIsRuletuple()
->()
,list("")
->[]
, etc.list()
(or tuple) call in comprehensions such as[x**2 for x in list(...)]
- we're about to iterate anyway. Only applies to list, set, or dict comprehensions; generator expressions might not consume the whole iterable.map(lambda ..., x)
with a comprehension, like Add C417 - Unnecessary use ofmap
adamchainz/flake8-comprehensions#409filter(lambda ..., x)
isinstance(x, y) or isinstance(x, z)
->isinstance(x, (y, z))
except ...:
clause just doesraise
, remove it and repeat for the preceeding clause. If it's the only except clause and there's nofinally:
, remove thetry:
block entirely.sorted(...)[0]
withmin(...)
(or[-1]
withmax(...)
). Invert min/max if passedreverse=True
(or truthy constant); ignorereverse=False
(or falsey); skip if thereverse=
argument is present with any non-constant value.Variously inspired by linters (
flake8
,flake8-comprehensions
,fixit
, etc.) and my own observations of some typed code.The text was updated successfully, but these errors were encountered: