org.sonar.l10n.py.rules.python.S6546.html Maven / Gradle / Ivy
Why is this an issue?
Python 3.10 introduced a specific syntax using the "or" operator (X | Y
) to represent a union of types. This syntax has the same
functionality as typing.Union
, but it is more concise and easier to read.
Using typing.Union
is more verbose and less convenient. It can also create inconsistencies when different parts of the codebase use
different syntaxes for the same type.
How to fix it
Replace usages of typing.Union
with the union type syntax.
Code examples
Noncompliant code example
from typing import Union
def foo(arg: Union[int, str]) -> Union[int, str]:
if isinstance(arg, int):
return arg + 1
else:
return arg.upper()
Compliant solution
def foo(arg: int | str) -> int | str:
if isinstance(arg, int):
return arg + 1
else:
return arg.upper()
Resources
Documentation
© 2015 - 2024 Weber Informatics LLC | Privacy Policy