Loading...
Loading...
Python >= 3.11 coding standards. Use when writing Python code. Requires type hints, return types, Google-style docstrings, trailing commas, explicit kwargs, StrEnum with auto(), dataclasses, pytest-style tests.
npx skill4agent add lgtm-hq/ai-skills stand-pyuvX | YUnion[X, Y]Selfdef function_with_docstring(
param1: str,
param2: int,
) -> bool:
"""Short description of function.
Longer description if needed.
Args:
param1: Description of param1.
param2: Description of param2.
Returns:
Description of return value.
Raises:
ValueError: When something is wrong.
"""dataclassescollections.defaultdictdataclassdefaultdictStrEnumauto()auto()any()all()# Don't
found = False
for p in paths:
if p.exists():
found = True
break
# Do
found = any(p.exists() for p in paths)dict.get()# Don't
if name in registry:
return registry[name]
return None
# Do
return registry.get(name)pathlibos.path# Don't
root = os.path.dirname(os.path.dirname(os.path.dirname(path)))
# Do
root = Path(path).parents[2]# Don't
if len(items) == 0:
...
# Do
if not items:
...# Don't
names = []
for user in users:
names.append(user.name)
# Do
names = [user.name for user in users]# Don't
if count > limit:
return True
return False
# Do
return count > limitcontextlib.suppress(SomeError)tryexcept SomeError: passitertoolsfunctoolschainpairwisecachereduce# Good
def foo(bar: str, baz: int,) -> None:
# Bad
def foo(bar: str, baz: int) -> None:# Good
foo(bar=bar, baz=baz)
# Bad
foo(bar, baz)# OK
foo(bar)lintlint# nosec# nosec BXXX - reason# noseclint# type: ignore# Don't - blanket, unjustified ignore
subprocess.run(["validate.sh"]) # nosec
# Do - narrowest code, inline justification
subprocess.run(["validate.sh"]) # nosec B603 - fixed argv list; no shellconftest.py@pytest.mark.parametrizeassertpyassertpytest.raises# Don't
assert result.count == 3
assert "drift" in output
# Do
from assertpy import assert_that
assert_that(result.count).is_equal_to(3)
assert_that(output).contains("drift")# WRONG
class TestFoo(unittest.TestCase):
def test_bar(self):
...
# CORRECT
def test_foo_bar() -> None:
"""Verify foo handles bar correctly."""
...field == "literal""literal"attr=min_versionmin_version_is_0.43.0# WRONG — id encodes the data, churns on every bump
ids=["min_version_is_0.43.0"]
# CORRECT — id names the case
ids=["attr=min_version"]