Skip to content
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

Merged
merged 9 commits into from
Jul 22, 2022
Merged

Conversation

tdimiduk
Copy link
Contributor

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.

@tdimiduk
Copy link
Contributor Author

Closing because it would break people who rely on this behavior.

Track this issue instead: #16193

@tdimiduk tdimiduk closed this Jul 15, 2022
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.
@tdimiduk
Copy link
Contributor Author

#16193

@Eric-Arellano Eric-Arellano changed the title Remove hardcoded argument from autoflake linter Allow using --import option with Autoflake Jul 15, 2022
@Eric-Arellano Eric-Arellano added category:new feature category:bugfix Bug fixes for released features and removed category:bugfix Bug fixes for released features category:new feature labels Jul 15, 2022
@tdimiduk
Copy link
Contributor Author

As a local test of this, I edited my pants.toml to contain

[autoflake]
args = [] 

and ran ./pants -ldebug fmt ::

We can see here that --remove-all-unused-imports is not present.

[DEBUG] spawned local process as Some(67697) for Process { argv: ["./autoflake.pex_pex_shim.sh", "--in-place", "src/python/pants/backend/helm/util_rules/sources_test.py", ...

where without the edit to pants.toml

[DEBUG] spawned local process as Some(68035) for Process { argv: ["./autoflake.pex_pex_shim.sh", "--in-place", "--remove-all-unused-imports", "src/python/pants/backend/scala/subsystems/scalatest.py", 

--remove-all-unused-imports does appear as before

Copy link
Contributor

@benjyw benjyw left a 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,
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Member

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

Copy link
Member

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?

Copy link
Member

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:

Suggested change
default: list[str] | None = None,
default: _MaybeDynamicT[list[str]] = [],

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@Eric-Arellano Eric-Arellano Jul 19, 2022

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.

Copy link
Member

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?

Copy link
Contributor

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

Copy link
Contributor Author

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>
Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again!

@Eric-Arellano Eric-Arellano added this to the 2.13.x milestone Jul 21, 2022
@Eric-Arellano Eric-Arellano enabled auto-merge (squash) July 21, 2022 21:10
@@ -802,6 +803,7 @@ def __new__(
"""
)
),
default=default,
Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. File an issue to mypy?
  2. We can type ignore and reference it here

Hopefully callsites are still type checked correctly

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Member

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.

auto-merge was automatically disabled July 21, 2022 22:29

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]] = [],
Copy link
Contributor Author

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.

Copy link
Contributor

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]
Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@stuhood stuhood enabled auto-merge (squash) July 22, 2022 17:58
@Eric-Arellano
Copy link
Contributor

Looks like a Black failure. I pushed the fix.

@stuhood stuhood merged commit 14e0122 into pantsbuild:main Jul 22, 2022
@tdimiduk
Copy link
Contributor Author

Looks like a Black failure. I pushed the fix.

Thanks.

@benjyw
Copy link
Contributor

benjyw commented Jul 22, 2022

Thanks again for the fix!

Eric-Arellano pushed a commit to Eric-Arellano/pants that referenced this pull request Jul 22, 2022
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
Eric-Arellano added a commit that referenced this pull request Jul 22, 2022
…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>
jyggen pushed a commit to jyggen/pants that referenced this pull request Jul 27, 2022
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:bugfix Bug fixes for released features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants