ruff
ruff copied to clipboard
T201: limit effectiveness in `__name__ = __main__` blocks
Hi all! As always, thanks for ruff.
This is a potential feature request, but a pretty minor one. Please close if you don't think it's worthwhile.
Observed behavior
I have a ruleset enabled that includes T201, which in turn forbids use of print(...). This is generally a reasonable rule.
At the same time, I want to intentionally use print(...) in some contexts where doing so is idiomatic, e.g. the __main__ scope when a module is invoked as a script:
print("warning expected here")
if __name__ == "__main__":
print("no warning expected here")
As is, both of these print(...)s trigger T201.
Expected behavior
The current default is reasonable.
However, it would be nice to have some kind of flag or option to allow T201 in the __main__ pseudo-scope. With that option, only the first print(...) would trigger, while the second would be allowed.
That sounds reasonable. Would you expect that all print calls inside of a __main__ block are allowed, even if they are eg. nested inside of a function or class?
if __name__ == "__main__":
def why_not():
print("Why?")
why_not()
That sounds reasonable. Would you expect that all
__main__block are allowed, even if they are eg. nested inside of a function or class?
Yep, that's my thinking -- I think any print() that is in a lexical scope of __main__ should be allowed.