Visitar URL original
plugins: delete open plugin by hauntsaninja · Pull Request #9275 · python/mypy · GitHub
The Wayback Machine - https://web.archive.org/web/20221225084400/https://github.com/python/mypy/pull/9275
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

plugins: delete open plugin #9275

Merged
merged 8 commits into from Feb 9, 2022
Merged

Conversation

hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Aug 7, 2020

Improvements to typeshed types using literals should have rendered this
unnecessary.

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Aug 7, 2020

Draft PR because we'll need python/typeshed#4407 to be merged and synced before tests will pass.

Copy link
Member

@gvanrossum gvanrossum left a comment

I expect this is going to make a lot of people unhappy — being more strict it will cause new failures in people’s CI. @JukkaL what do you think? Would it be bad for the Dropbox build?

finally:
if not f.closed:
f.close()
support.unlink(support.TESTFN)
Copy link
Member

@gvanrossum gvanrossum Aug 7, 2020

Choose a reason for hiding this comment

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

Is it safe to drop the finally clause with unlink?

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Aug 7, 2020 •

Choose a reason for hiding this comment

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

It wouldn't be, if this code were ever executed! We've talked about entirely getting rid of the stdlib samples before (e.g., in #8838), so I didn't try very hard to preserve them.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 7, 2020

@JukkaL what do you think? Would it be bad for the Dropbox build?

It's unclear, but I wouldn't be surprised if this requires quite a few changes, as there are thousands of calls to open. I can test this against Dropbox internal repos once the build is green.

@@ -243,10 +243,10 @@ def _write_out_report(self,
f.write(separator + '\n')
for row_values in rows:
r = ("{:>{}}" * len(widths)).format(*itertools.chain(*zip(row_values, widths)))
f.writelines(r + '\n')
f.write(r + '\n')
Copy link
Member

@JelleZijlstra JelleZijlstra Aug 7, 2020

Choose a reason for hiding this comment

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

What made this change necessary?

Copy link
Member

@gvanrossum gvanrossum Aug 7, 2020

Choose a reason for hiding this comment

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

That looks like it fixes a bug! writelines takes a sequence of strings, not a single string (though because a string is a sequence of strings and writelines just writes the concatenation of the items it worked :-).

Though I'm not sure how the change found this bug -- does the TextIoWriter class (or whatever is used) not have writelines?

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Aug 7, 2020

Choose a reason for hiding this comment

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

We now infer f as TextIOWrapper. TextIOWrapper and TextIOBase take List[str] for writelines in typeshed. Maybe that signature should be loosened though.

Copy link
Member

@JelleZijlstra JelleZijlstra Aug 7, 2020

Choose a reason for hiding this comment

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

Yes, I think that should be Iterable[str]. I just tried in 3.6 and TextIOWrapper.writelines accepts at least tuples and generator expressions.

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Aug 7, 2020

Choose a reason for hiding this comment

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

And it looks like the perennial "str is a iterable of str" bug is benign in this case, so there's not really any downside to the looser signature.

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Aug 22, 2020

All green now! I'm not sure what will become of this PR, but it's ready to find out!

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Sep 26, 2020

I wrote a script that democratises the ability to evaluate changes like this: https://github.com/hauntsaninja/mypy_primer

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Sep 26, 2020 •

Here are the results for this PR:

Fixed a false positive:
- zerver/lib/cache.py:97: error: Incompatible types in assignment (expression has type "TextIO", variable has type "TextIOWrapper")  [assignment]

Exposes an incorrect type in typeshed:
+ sphinx/ext/coverage.py:250:38: error: Argument 1 to "writelines" of "TextIOWrapper" has incompatible type "Generator[str, None, None]"; expected "List[str]"
+ sphinx/ext/coverage.py:279:46: error: Argument 1 to "writelines" of "TextIOWrapper" has incompatible type "Generator[str, None, None]"; expected "List[str]"
+ sphinx/ext/coverage.py:297:30: error: Argument 1 to "writelines" of "TextIOWrapper" has incompatible type "Generator[str, None, None]"; expected "List[str]"

Finds a "bug" (but also would go away by loosening the above type in typeshed):
+ mypy/report.py:246: error: Argument 1 to "writelines" of "TextIOWrapper" has incompatible type "str"; expected "List[str]"  [arg-type]
+ mypy/report.py:249: error: Argument 1 to "writelines" of "TextIOWrapper" has incompatible type "str"; expected "List[str]"  [arg-type]
+ tools/lib/provision_inner.py:79: error: Argument 1 to "writelines" of "TextIOWrapper" has incompatible type "str"; expected "List[str]"  [arg-type]
+ tools/lib/provision_inner.py:82: error: Argument 1 to "writelines" of "TextIOWrapper" has incompatible type "str"; expected "List[str]"  [arg-type]

Presumed false positive (although intentional on mypy's part, in the absence of --allow-redefinition):
+ src/pip/_internal/req/req_uninstall.py:644: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")
+ src/pip/_internal/operations/install/wheel.py:126: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")
+ mypyc/build.py:304: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")  [assignment]
+ tools/setup/generate_integration_bots_avatars.py:52: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")  [assignment]
+ pandas/io/stata.py:1063: error: Incompatible types in assignment (expression has type "BytesIO", variable has type "BufferedReader")  [assignment]

If we loosen the type of writelines in typeshed (as we should), this brings the count to five errors against (one of which is fixed in this PR) and one error for out of a couple million lines of code. The five errors against are arguably "by design", since --allow-redefinition is not the default.

hauntsaninja added 5 commits Sep 28, 2020
Improvements to typeshed types using literals should have rendered this
unnecessary.
Since errors shouldn't be used with binary modes. Maybe should just
delete?
hauntsaninja pushed a commit to hauntsaninja/typeshed that referenced this pull request Oct 10, 2020
hauntsaninja added a commit to python/typeshed that referenced this pull request Oct 10, 2020
This came up in python/mypy#9275

Co-authored-by: hauntsaninja <>
@msullivan
Copy link
Collaborator

msullivan commented Oct 18, 2020

There's a competing "delete open plugin" from earlier: #7794.

Do you have any opinions on this one versus that, @hauntsaninja

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Oct 18, 2020 •

Yeah, I noticed the duplication a couple weeks ago when cleaning up some old PRs :-/

The big difference is that that PR no longer gets rid of the open plugin, because of https://github.com/python/mypy/pull/7794/files#discussion_r342519954
My opinion is that raising an error for the errors='replace' + binary case doesn't pull the extra weight of having a plugin.

It's also not clear to me why that PR doesn't necessitate some of the changes that I've made here. It's possible merging in a newer typeshed into that PR would surface those complaints (although I'll note that I changed the writelines type in typeshed in python/typeshed#4642)

@msullivan
Copy link
Collaborator

msullivan commented Oct 18, 2020

@JukkaL @ilevkivskyi what is the damage like for this PR at Dropbox?

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2022

Diff from mypy_primer, showing the effect of this PR on open source code:

mypy_primer (https://github.com/hauntsaninja/mypy_primer)
+ mypy_primer.py:118: error: Unused "type: ignore" comment
+ mypy_primer.py:119: error: Item "None" of "Optional[bytes]" has no attribute "count"

mypy (https://github.com/python/mypy)
+ mypyc/build.py:311: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")  [assignment]

pip (https://github.com/pypa/pip)
+ src/pip/_internal/req/req_uninstall.py:623: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")
+ src/pip/_internal/operations/install/wheel.py:107: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")

zulip (https://github.com/zulip/zulip)
+ tools/setup/generate_integration_bots_avatars.py:54: error: Incompatible types in assignment (expression has type "BufferedWriter", variable has type "BufferedReader")  [assignment]

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Feb 9, 2022

Issues that this would fix are reported fairly regularly. Given how long this PR has been lying around, and that the mypy_primer output doesn't look unworkable, I'm going to try asking for forgiveness on this merge :-)

@hauntsaninja hauntsaninja merged commit 0647570 into python:master Feb 9, 2022
15 checks passed
@hauntsaninja hauntsaninja deleted the opplug branch Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mypy does not honour open() overrides from typeshed.
5 participants