astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.6k stars 1.09k forks source link

Allow banning of certain builtins in "banned-api" #10079

Open jonathanslenders opened 8 months ago

jonathanslenders commented 8 months ago

I'd love to ban usage of certain builtins using the "banned-api" (TID251) rule. Something like this:

[tool.ruff.lint.flake8-tidy-imports.banned-api]
# Don't use delattr/getattr/setattr -> too dynamic!
"__builtins__.delattr".msg = "Don't use delattr/setattr/getattr."
"__builtins__.setattr".msg = "Don't use delattr/setattr/getattr."
"__builtins__.getattr".msg = "Don't use delattr/setattr/getattr."

# Don't use `@property`, because methods are more flexible and allow access to unbound methods.
"__builtins__.property".msg = "Don't use properties in this part of the code."

# Don't print. Basically the same as the `T` rule.
"__builtins__.print".msg = "Don't use print."

"__builtins__.list".msg = "Use list literal instead."
KotlinIsland commented 4 months ago

I would prefer builtins.eggs over __builtins__.eggs, that's the actual name of the module, and:

As an implementation detail, most modules have the name __builtins__ made available as part of their globals. The value of __builtins__ is normally either this module or the value of this module’s __dict__ attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python. - https://docs.python.org/3/library/builtins.html