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

Blank line between nested and function def in stub files. #3862

Merged
merged 2 commits into from
Sep 9, 2023

Commits on Sep 8, 2023

  1. Blank line between nested and function def in stub files.

    The idea behind this change is that we stop looking into previous body to determine if there should be a blank before a function or class definition.
    
    Input:
    
    ```python
    import sys
    
    if sys.version_info > (3, 7):
        class Nested1:
            assignment = 1
            def function_definition(self): ...
        def f1(self) -> str: ...
        class Nested2:
            def function_definition(self): ...
            assignment = 1
        def f2(self) -> str: ...
    
    if sys.version_info > (3, 7):
        def nested1():
            assignment = 1
            def function_definition(self): ...
        def f1(self) -> str: ...
        def nested2():
            def function_definition(self): ...
            assignment = 1
        def f2(self) -> str: ...
    ```
    
    Stable style
    ```python
    import sys
    
    if sys.version_info > (3, 7):
        class Nested1:
            assignment = 1
            def function_definition(self): ...
    
        def f1(self) -> str: ...
    
        class Nested2:
            def function_definition(self): ...
            assignment = 1
        def f2(self) -> str: ...
    
    if sys.version_info > (3, 7):
        def nested1():
            assignment = 1
            def function_definition(self): ...
    
        def f1(self) -> str: ...
        def nested2():
            def function_definition(self): ...
            assignment = 1
        def f2(self) -> str: ...
    ```
    
    In the stable formatting, we have a blank line sometimes, not depending on the previous statement on the same level, but on the last (potentially nested) statement in the previous body.
    
    psf#2783/psf#3564 fixes this for classes in preview style:
    
    ```python
    import sys
    
    if sys.version_info > (3, 7):
        class Nested1:
            assignment = 1
            def function_definition(self): ...
    
        def f1(self) -> str: ...
    
        class Nested2:
            def function_definition(self): ...
            assignment = 1
    
        def f2(self) -> str: ...
    
    if sys.version_info > (3, 7):
        def nested1():
            assignment = 1
            def function_definition(self): ...
    
        def f1(self) -> str: ...
        def nested2():
            def function_definition(self): ...
            assignment = 1
        def f2(self) -> str: ...
    ```
    
    This PR additionally fixes this for function definitions:
    
    ```python
    if sys.version_info > (3, 7):
        if sys.platform == "win32":
            assignment = 1
            def function_definition(self): ...
    
        def f1(self) -> str: ...
        if sys.platform != "win32":
            def function_definition(self): ...
            assignment = 1
    
        def f2(self) -> str: ...
    
    if sys.version_info > (3, 8):
        if sys.platform == "win32":
            assignment = 1
            def function_definition(self): ...
    
        class F1: ...
        if sys.platform != "win32":
            def function_definition(self): ...
            assignment = 1
    
        class F2: ...
    ```
    
    You can see the effect of this change on typeshed in https://github.com/konstin/typeshed/pull/1/files. As baseline, the preview mode changes without this PR are at konstin/typeshed#2.
    konstin committed Sep 8, 2023
    Configuration menu
    Copy the full SHA
    9e1cc30 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    870f12c View commit details
    Browse the repository at this point in the history