-
-
Notifications
You must be signed in to change notification settings - Fork 636
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
Allow using --import
option with Autoflake
#16192
Conversation
Closing because it would break people who rely on this behavior. Track this issue instead: #16193 |
Move "--remove-all-unused-imports" to a default argument rather than a hardcoded portion of the command for autoflake. This allows a user to override if the'd prefer a more conservative run of autoflake.
--import
option with Autoflake
As a local test of this, I edited my
and ran We can see here that
where without the edit to pants.toml
|
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.
Nice! Thanks for this improvement.
@@ -789,6 +789,7 @@ def __new__( | |||
# This should be set when callers can alternatively use "--" followed by the arguments, | |||
# instead of having to provide "--[scope]-args='--arg1 --arg2'". | |||
passthrough: bool | None = None, | |||
default: list[str] | None = None, |
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.
The mypy error in CI leads me to believe that the type annotation here should be _MaybeDynamicT[list[_ListMemberT]]
. But @Eric-Arellano is more informed than I am about the types here.
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.
@thejcannon thoughts? I think the issue is the | None
? We should be able to not use _MaybeDynamicT
because this type hint can be a subset of the original union.
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'd want it to be maybe dynamic though. I agree with Benjy, although type hinting is hard so it might still 💥
Eric I don't quite understand your comment
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.
In the meantime, is there a workaround to unblock landing this?
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.
Is it blocked? I think this just needs to be changed to:
default: list[str] | None = None, | |
default: _MaybeDynamicT[list[str]] = [], |
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.
FWIW we do it above...
https://github.com/tdimiduk/pants/blob/b47224962aea8fc442c94bd3305242c82011b585/src/python/pants/option/option_types.py#L641
https://github.com/tdimiduk/pants/blob/b47224962aea8fc442c94bd3305242c82011b585/src/python/pants/option/option_types.py#L722
and most importantly: https://github.com/tdimiduk/pants/blob/b47224962aea8fc442c94bd3305242c82011b585/src/python/pants/option/option_types.py#L219
I agree it's dangerous. But we're never mutating the value (its just the default) so personally 🤷♂️
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.
Okay - this PR isn't the hill for me to die on then. We really appreciate your contribution @tdimiduk and it isn't your responsibility to fix something that was already not ideal
So, I think the best thing would be @tdimiduk to change the list[str] | None = None
type hint to be list[str] = []
. Then, run ./pants --changed-since=HEAD check
and double check you're good to go.
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.
Wouldn't the right thing to do be to use the suggestion above? Is there a reason we'd want to disallow dynamic 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.
Ah true. I merged the commit. Thanks
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.
Sorry for the delay in getting back to this. I got sick while traveling and am just catching up with things now.
I don't love the mutable []
as a default (and was what I clanked on finding a better solution to before I got sick). If you are happy with this (as it being not worse than is done elsewhere), I'm happy.
I'm seeing that suggestion applied, and so this should be ready to go with another run of the CI? Is there anything else I should do?
Co-authored-by: Joshua Cannon <joshdcannon@gmail.com>
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.
Thanks again!
@@ -802,6 +803,7 @@ def __new__( | |||
""" | |||
) | |||
), | |||
default=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.
Lol what. The new error message makes no sense to me. The types are the same according to the error. I assumed _ListMemberT
was the same name, but not the same value -- but that doesn't seem to be it.
src/python/pants/option/option_types.py: note: In member "__new__" of class "ArgsListOption":
[276](https://github.com/pantsbuild/pants/runs/7458162823?check_suite_focus=true#step:9:277)
src/python/pants/option/option_types.py:806:21: error: Argument "default" to
[277](https://github.com/pantsbuild/pants/runs/7458162823?check_suite_focus=true#step:9:278)
"__new__" of "_ListOptionBase" has incompatible type
[278](https://github.com/pantsbuild/pants/runs/7458162823?check_suite_focus=true#step:9:279)
"Union[Callable[[Any], Any], List[_ListMemberT]]"; expected
[279](https://github.com/pantsbuild/pants/runs/7458162823?check_suite_focus=true#step:9:280)
"Union[Callable[[Any], Any], List[_ListMemberT]]" [arg-type]
[280](https://github.com/pantsbuild/pants/runs/7458162823?check_suite_focus=true#step:9:281)
default=default,
[281](https://github.com/pantsbuild/pants/runs/7458162823?check_suite_focus=true#step:9:282)
^
At what point should we ignore the error?
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.
- File an issue to mypy?
- We can type ignore and reference it here
Hopefully callsites are still type checked correctly
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've gone with the type ignore for now. @Eric-Arellano or @thejcannon do you have more context for the mypy bug and want to file it? I can do it, but I'd need to spend some time figuring out what is going on here to file a useful bug.
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.
Weird right? This goes beyond my understanding of type annotations. I think a type ignore annotation is the way to go.
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 would expect mypy
devs to want us to use main
to reproduce, and bumping mypy in the repo isn't fun. Let's just leave it for now and really hope that its fixed in future versions and we get that "unnecessary type ignore" warning.
Head branch was pushed to by a user without write access
@@ -216,7 +216,7 @@ def __new__( | |||
cls, | |||
flag_name: str | None = None, | |||
*, | |||
default: _MaybeDynamicT[list[_ListMemberT]] = [], |
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.
Discovered there is already a check on line 234 to put in []
if default
is falsy. Seems like making the default here Optional
and passing in None
is preferable to using the mutable default argument. Happy to back it out if we don't want to change the signature here.
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.
Agreed, since we can't get the type check to work anyway, better to not do the bad thing with the mutable default for no purpose.
@@ -802,6 +803,7 @@ def __new__( | |||
""" | |||
) | |||
), | |||
default=default, # type: ignore[arg-type] |
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.
Went with the type ignore for now since it does seem like this might be a mypy bug.
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.
Thanks for your patience with all this! Mypy is a bit of a challenge sometimes.
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.
You are welcome. I'm glad to have typing in python, just eagery looking forward to it becoming better typing over time.
Looks like a Black failure. I pushed the fix. |
Thanks. |
Thanks again for the fix! |
The linter is currently hardcoded to do "--remove-all-unused-imports", which unfortunately does not work for all repos since imports can have side effects. With this removed a user can still provide the argument if they desire it, but it is possible to run without it. [ci skip-build-wheels] [ci skip-rust] # Conflicts: # src/python/pants/option/option_types.py
…16275) The linter is currently hardcoded to do "--remove-all-unused-imports", which unfortunately does not work for all repos since imports can have side effects. With this removed a user can still provide the argument if they desire it, but it is possible to run without it. [ci skip-build-wheels] [ci skip-rust] Co-authored-by: Tom Dimiduk <tom@dimiduk.net>
The linter is currently hardcoded to do "--remove-all-unused-imports", which unfortunately does not work for all repos since imports can have side effects. With this removed a user can still provide the argument if they desire it, but it is possible to run without it. [ci skip-build-wheels] [ci skip-rust]
The linter is currently hardcoded to do "--remove-all-unused-imports", which
unfortunately does not work for all repos since imports can have side effects.
With this removed a user can still provide the argument if they desire it, but
it is possible to run without it.