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

[SIM907] Use Optional[Type] instead of Union[Type, None] #64

Closed
kasium opened this issue Jun 16, 2021 · 3 comments · Fixed by #110
Closed

[SIM907] Use Optional[Type] instead of Union[Type, None] #64

kasium opened this issue Jun 16, 2021 · 3 comments · Fixed by #110
Assignees
Labels
enhancement New feature or request

Comments

@kasium
Copy link

kasium commented Jun 16, 2021

Explanation

Sometimes people tend to write Union[Type, None] instead of Optional[Type].

Example

# Bad
def foo(a: Union[int, None]) -> Union[int, None]:
  return a

# Good
def foo(a: Optional[int]) -> Optional[int]:
  return a
@kasium kasium added the enhancement New feature or request label Jun 16, 2021
@MartinThoma
Copy link
Owner

$ astpretty --no-show-offsets /dev/stdin <<< `cat example.py`
Module(
    body=[
        FunctionDef(
            name='foo',
            args=arguments(
                posonlyargs=[],
                args=[
                    arg(
                        arg='a',
                        annotation=Subscript(
                            value=Name(id='Union', ctx=Load()),
                            slice=Tuple(
                                elts=[
                                    Name(id='int', ctx=Load()),
                                    Constant(value=None, kind=None),
                                ],
                                ctx=Load(),
                            ),
                            ctx=Load(),
                        ),
                        type_comment=None,
                    ),
                ],
                vararg=None,
                kwonlyargs=[],
                kw_defaults=[],
                kwarg=None,
                defaults=[],
            ),
            body=[
                Return(
                    value=Name(id='a', ctx=Load()),
                ),
            ],
            decorator_list=[],
            returns=Subscript(
                value=Name(id='Union', ctx=Load()),
                slice=Tuple(
                    elts=[
                        Name(id='int', ctx=Load()),
                        Constant(value=None, kind=None),
                    ],
                    ctx=Load(),
                ),
                ctx=Load(),
            ),
            type_comment=None,
        ),
    ],
    type_ignores=[],
)

@MartinThoma MartinThoma changed the title [New Rule] Find Union[Type, None] [SIM907] Find Union[Type, None] Feb 27, 2022
@MartinThoma MartinThoma changed the title [SIM907] Find Union[Type, None] [SIM907] Use Optional[Type] instead of Union[Type, None] Feb 27, 2022
@Skylion007
Copy link
Contributor

Note: in 3.10+ PyUpgrade recommends using Type | None which translates to Union[Type, None] as this shorthand is shorter than Optional[Type] , so I wonder how useful this check would actually be.

@MartinThoma
Copy link
Owner

Thank you! I'll try to find a way to disable it for Python 3.10+ :-) (should be possible with sys.version)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants