Skip to content

Suggestion - checker that detects magic numbers/unnamed constants #7281

Closed
@orSolocate

Description

@orSolocate

Current problem

I thought it would be nice to have a checker that detects magic numbers (aka. unnamed constants) in Pylint.

As in all standard languages, -1, 0 and 1 are allowed by default. Other numbers should raise a message unless would be specified in the pylintrc file.

As not all might want to use it and since it is just really a bad practice and never an error it should be considered as an extension instead of core checker.

According to my test there is nothing like that in Pylint, but I'd like to be proven wrong :)

Example:

seconds = 60

func(a, b, 50)

shows no error related to magic number

Desired solution

1: seconds = 60

2: func(a, b, 50)

should show 2 messages of type Refactor (it is never an error just a bad practice in my opinion).

1: "Unnamed constant '60' is used, consider using literal instead of magic numbers."
2: "Unnamed constant '50' is used, consider using literal instead of magic numbers."

Additional context

Similar issue was opened but it was a bit different and more complicated than what I suggest here, see #1379

Similar checkers exist in ESlint and TSlint.

Metadata

Metadata

Assignees

Labels

Enhancement ✨Improvement to a componentNeeds design proposal 🔒This is a huge feature, some discussion should happen before a PR is proposedchecker-CodeStyleIssues and PRs that relate to the `CodeStyleChecker`

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions