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

Single line to multi line change by black causes different type hint scope #1640

Closed
dveeden opened this issue Aug 27, 2020 · 1 comment
Closed
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: bug Something isn't working

Comments

@dveeden
Copy link

dveeden commented Aug 27, 2020

Describe the bug A clear and concise description of what the bug is.

When reformatting a single line into a multi line statement the scope of type hints changes.
This is with the default --safe

To Reproduce Steps to reproduce the behavior:

  1. Take this file '...'
import requests


class Foo:
    def __init__(self) -> None:
        self.session = requests.Session()

    def bar(self) -> None:
        response = self.session.request(method="GET", url="http://httpbin.org/status/200", params={},)  # type: ignore
        print(response.status_code)


foo = Foo()
foo.bar()
  1. Run Black on it with these arguments '....'
    Without arguments

  2. See error

Expected behavior A clear and concise description of what you expected to happen.

I would expect black to take the scope of type hints in consideration and either:

  • Move/change type hints to have them keep the same scope
  • Decide to not touch these lines
  • Give a warning/error about this not being safe

Environment (please complete the following information):

  • Version: 20.8b1
  • OS and Python version: [e.g. Linux/Python 3.7.4rc1]
    Linux/Python 3.8.5

Does this bug also happen on master? To answer this, you have two options:

Yes

Additional context Add any other context about the problem here.

Current behaviour:

-        response = self.session.request(method="GET", url="http://httpbin.org/status/200", params={},)  # type: ignore
+        response = self.session.request(
+            method="GET",
+            url="http://httpbin.org/status/200",
+            params={},
+        )  # type: ignore

One possible solution (a bit ugly, but keeps the same scope)

-        response = self.session.request(method="GET", url="http://httpbin.org/status/200", params={},)  # type: ignore
+        response = self.session.request(   # type: ignore
+            method="GET",   # type: ignore
+            url="http://httpbin.org/status/200",   # type: ignore
+            params={},   # type: ignore
+        )  # type: ignore
@dveeden dveeden added the T: bug Something isn't working label Aug 27, 2020
@ichard26 ichard26 added the F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. label Aug 27, 2020
@JelleZijlstra
Copy link
Collaborator

I'm going to say that we shouldn't change this behavior:

  • We already have special handling for type comments that usually avoids moving them around
  • Putting a type ignore comment on every line, as suggested above, would be ugly and would likely require manual work from the user anyway (e.g., with the --warn-unused-ignores mypy flag).
  • Recent releases of mypy scope a # type: ignore to the whole logical line, so the current behavior should already work.

I also just submitted #2272 clarifying this behavior in the FAQ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants