Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add "no math constants" check #176

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/categories.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ These checks cover:
These checks relate to the [itertools](https://docs.python.org/3/library/itertools.html)
standard library module.

## `math`

These checks relate to the [math](https://docs.python.org/3/library/math.html)
standard library module.

## `operator`

These checks relate to the [operator](https://docs.python.org/3/library/operator.html)
Expand Down
Empty file added refurb/checks/math/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions refurb/checks/math/use_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from dataclasses import dataclass

from mypy.nodes import FloatExpr

from refurb.error import Error


@dataclass
class ErrorInfo(Error):
"""
Don't hardcode math constants like pi, tau, or e, use the `math.pi`,
`math.tau`, or `math.e` constants respectively.

Bad:

```
def area(r: float) -> float:
return 3.1415 * r * r
```

Good:

```
import math

def area(r: float) -> float:
return math.pi * r * r
```
"""

code = 152
categories = ["math", "readability"]


CONSTANTS = {
"pi": "3.14",
"e": "2.71",
"tau": "6.28",
}


def check(node: FloatExpr, errors: list[Error]) -> None:
num = str(node.value)

if len(num) <= 3:
return None

for name, value in CONSTANTS.items():
if num.startswith(value):
errors.append(
ErrorInfo(
node.line,
node.column,
f"Replace `{num}` with `math.{name}`",
)
)
4 changes: 2 additions & 2 deletions test/data/err_123.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
_ = bytes(b"hello world")
_ = complex(1j)
_ = dict({"a": 1})
_ = float(3.14)
_ = float(123.456)
_ = list([1, 2, 3])
_ = str("hello world")
_ = tuple((1, 2, 3))
Expand All @@ -22,7 +22,7 @@
d = {"a": 1}
_ = dict(d)

e = 3.14
e = 123.456
_ = float(e)

f = [1, 2, 3]
Expand Down
22 changes: 22 additions & 0 deletions test/data/err_152.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# these should match

pi = 3.14
pi = 3.1415
pi = 003.1400
pi = -3.14
e = 2.71
e = 2.71828
e = -2.71
tau = 6.28
tau = 6.2831
tau = 2 * 3.14
tau = -6.28


# these should not

_ = 3.1
_ = 2.7
_ = 6.2
_ = 1234
_ = 3.0
11 changes: 11 additions & 0 deletions test/data/err_152.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test/data/err_152.py:3:6 [FURB152]: Replace `3.14` with `math.pi`
test/data/err_152.py:4:6 [FURB152]: Replace `3.1415` with `math.pi`
test/data/err_152.py:5:6 [FURB152]: Replace `3.14` with `math.pi`
test/data/err_152.py:6:7 [FURB152]: Replace `3.14` with `math.pi`
test/data/err_152.py:7:5 [FURB152]: Replace `2.71` with `math.e`
test/data/err_152.py:8:5 [FURB152]: Replace `2.71828` with `math.e`
test/data/err_152.py:9:6 [FURB152]: Replace `2.71` with `math.e`
test/data/err_152.py:10:7 [FURB152]: Replace `6.28` with `math.tau`
test/data/err_152.py:11:7 [FURB152]: Replace `6.2831` with `math.tau`
test/data/err_152.py:12:11 [FURB152]: Replace `3.14` with `math.pi`
test/data/err_152.py:13:8 [FURB152]: Replace `6.28` with `math.tau`