Skip to content

Commit f8f2987

Browse files
[python] Add pyproject and lint with ruff
1 parent 6da0164 commit f8f2987

File tree

7 files changed

+96
-39
lines changed

7 files changed

+96
-39
lines changed

pyproject.toml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[tool.ruff]
2+
line-length = 120
3+
target-version = "py310"
4+
5+
[tool.ruff.lint]
6+
select = [
7+
"F", # Pyflakes
8+
"E", # pycodestyle errors
9+
"W", # pycodestyle warnings
10+
"C90", # mccabe complexity
11+
"I", # isort
12+
"N", # pep8-naming
13+
"UP", # pyupgrade
14+
"ANN", # flake8-annotations
15+
"ASYNC", # flake8-async
16+
"S", # flake8-bandit
17+
"BLE", # flake8-blind-except
18+
"B", # flake8-bugbear
19+
"A", # flake8-builtins
20+
"C4", # flake8-comprehensions
21+
"DTZ", # flake8-datetimez
22+
"T10", # flake8-debugger
23+
"EM", # flake8-errmsg
24+
"ISC", # flake8-implicit-str-concat
25+
"ICN", # flake8-import-conventions
26+
"G", # flake8-logging-format
27+
"PIE", # flake8-pie
28+
"T20", # flake8-print
29+
"PT", # flake8-pytest-style
30+
"Q", # flake8-quotes
31+
"RSE", # flake8-raise
32+
"RET", # flake8-return
33+
"SIM", # flake8-simplify
34+
"TID", # flake8-tidy-imports
35+
"ARG", # flake8-unused-arguments
36+
"PTH", # flake8-use-pathlib
37+
"ERA", # eradicate
38+
"PL", # Pylint
39+
"PERF", # Perflint
40+
"RUF", # Ruff-specific rules
41+
]
42+
ignore = [
43+
"D203", # one-blank-line-before-class (conflicts with D211)
44+
"D213", # multi-line-summary-second-line (conflicts with D212)
45+
"T201", # print statement used
46+
"PLR2004", # constant variable
47+
]
48+
49+
[tool.ruff.lint.per-file-ignores]
50+
"**/__init__.py" = ["D104"] # missing docstring in public package
51+
"**/test_*.py" = ["S101", "D"] # allow assert in tests, skip docstrings
52+
"**/tests/**/*.py" = ["S101", "D"] # allow assert in tests, skip docstrings
53+
54+
[tool.ruff.lint.pydocstyle]
55+
convention = "google"
56+
57+
[tool.ruff.lint.mccabe]
58+
max-complexity = 10
59+
60+
[tool.ruff.lint.isort]
61+
known-first-party = ["scenarios"]

scenarios/python_asyncio_3.11/main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import os
21
import asyncio
2+
import os
3+
4+
from ddtrace.profiling import Profiler
35

46

57
async def my_coroutine(n: float) -> None:
@@ -21,20 +23,18 @@ async def main() -> None:
2123
# We are in the process of making asyncio better in dd-trace-py; we will update the correctness check once that
2224
# issue is fixed.
2325

24-
from ddtrace.profiling import Profiler
25-
2626
prof = Profiler()
2727
prof.start() # Should be as early as possible, eg before other imports, to ensure everything is profiled
2828

2929
# Give the Profiler some time to start up
3030
await asyncio.sleep(0.5)
3131

32-
EXECUTION_TIME_SEC = float(os.environ.get("EXECUTION_TIME_SEC", "2"))
32+
execution_time_sec = float(os.environ.get("EXECUTION_TIME_SEC", "2"))
3333

34-
short_task = asyncio.create_task(my_coroutine(EXECUTION_TIME_SEC / 2))
34+
short_task = asyncio.create_task(my_coroutine(execution_time_sec / 2))
3535

3636
# asyncio.gather will automatically wrap my_coroutine into a Task
37-
await asyncio.gather(short_task, my_coroutine(EXECUTION_TIME_SEC))
37+
await asyncio.gather(short_task, my_coroutine(execution_time_sec))
3838

3939

4040
if __name__ == "__main__":

scenarios/python_basic_3.11/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
22
from threading import Thread
33
from time import sleep
4+
45
from ddtrace.profiling import Profiler
56

67

7-
def target(n):
8+
def target(n: int) -> None:
89
sleep(n)
910

1011

scenarios/python_basic_gevent/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
monkey.patch_all()
44

55
import os # noqa: E402
6+
import time # noqa: E402
67
from threading import Thread # noqa: E402
78

89

910
def target(n: int) -> None:
1011
# Do actual work instead of just sleeping so profiler can capture it
11-
import time
12-
1312
end_time = time.monotonic() + n
1413
count = 0
1514
while time.monotonic() < end_time:

scenarios/python_cpu/expected_profile.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"profile-type": "cpu-time",
66
"stack-content": [
77
{
8-
"regular_expression": "<module>;main;b",
8+
"regular_expression": "<module>;.*main;.*b",
99
"percent": 66,
1010
"error_margin": 5,
1111
"labels": [
@@ -18,7 +18,7 @@
1818
]
1919
},
2020
{
21-
"regular_expression": "<module>;main;a",
21+
"regular_expression": "<module>;.*main;.*a",
2222
"percent": 33,
2323
"error_margin": 6,
2424
"labels": [

scenarios/python_cpu/main.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
import os
22
from time import time
33

4-
x = 0
5-
i = 0
64

5+
class CPUBurner:
6+
def __init__(self) -> None:
7+
self.x = 0
8+
self.i = 0
79

8-
def main():
9-
global x, i
10-
EXECUTION_TIME_SEC = int(
11-
os.getenv("EXECUTION_TIME_SEC", "10")
12-
) # defaults to 10 if not set
13-
end = time() + EXECUTION_TIME_SEC
14-
while time() < end:
15-
a()
16-
b()
17-
# We add a print to prevent optimization that could turn this into a no-op program
18-
print(x)
10+
def a(self) -> None:
11+
self.i = 0
12+
while self.i < 1000000:
13+
self.x += self.i
14+
self.i += 1
1915

16+
def b(self) -> None:
17+
self.i = 0
18+
while self.i < 2000000:
19+
self.x += self.i
20+
self.i += 1
2021

21-
def a():
22-
global x, i
23-
i = 0
24-
while i < 1000000:
25-
x += i
26-
i += 1
22+
def main(self) -> None:
23+
execution_time_sec = int(os.getenv("EXECUTION_TIME_SEC", "10")) # defaults to 10 if not set
24+
end = time() + execution_time_sec
25+
while time() < end:
26+
self.a()
27+
self.b()
2728

29+
# We add a print to prevent optimization that could turn this into a no-op program
30+
print(self.x)
2831

29-
def b():
30-
global x, i
31-
i = 0
32-
while i < 2000000:
33-
x += i
34-
i += 1
3532

36-
37-
main()
33+
CPUBurner().main()

scenarios/python_uwsgi_3.11/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def _make_requests() -> None:
12-
run_time = int(os.environ.get("EXECUTION_TIME_SEC", 10))
12+
run_time = int(os.environ.get("EXECUTION_TIME_SEC", "10"))
1313

1414
start = time.monotonic()
1515
while time.monotonic() - start < run_time:

0 commit comments

Comments
 (0)