Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Does not recognize additional --builtins #73

Closed
kthy opened this issue Feb 2, 2022 · 5 comments · Fixed by #99
Closed

Does not recognize additional --builtins #73

kthy opened this issue Feb 2, 2022 · 5 comments · Fixed by #99

Comments

@kthy
Copy link

kthy commented Feb 2, 2022

As per the docs, it is possible to add builtins to flake8 by passing the --builtins flag or through config files.

However, these are not recognized by flake8-builtins:

import gettext

gettext.install("minimal_repro")


def id(s):
    return s


def _(s):
    return s


print(id("foo"))
print(_("bar"))
$ python ./no_custom_builtins.py
foo
bar
$ flake8 ./no_custom_builtins.py --builtins=_
./no_custom_builtins.py:1:1: D100 Missing docstring in public module
./no_custom_builtins.py:6:1: A001 variable "id" is shadowing a python builtin
./no_custom_builtins.py:6:1: D103 Missing docstring in public function

A001 should be reported for both functions.

@gforcada
Copy link
Owner

gforcada commented Feb 7, 2022

@kthy thanks for the report and for using flake8-builtins! ✨ I'm sorry to answer so late, my real life is rather busy as of late 😅

I'm not sure if we are mixing things here 🤔

flake8-builtins looks for what's on the python standard library and reports re-definitions of them, so on your example def id gets a warning about it. def _ though does not, as the python standard library does not have a underscore function.

This configuration option from flake8 that you point to the documentation, if I read it correctly, I understand it as that if you pass --builtins=my_custom_function to flake8 and then have the following code:

import random

random.choice(my_custom_function())

flake8 would not complain that you did not import my_custom_function as is a forced builtin.

In your example, as you are doing def _() and the underscore function is not a builtin you don't get a warning from flake8-builtins nor from flake8 itself as you define it. If you remove the def _ and re-run flake8 you will see a warning from flake8 itself. If then you add --builtins=_ when invoking flake8 that warning should go away.

Let me know if I got it wrong!

On the other hand though, we could add support in flake8-builtins to also warn if one overrides builtins provided via --builtins flake8 parameter/configuration. Would that be something you wish to have in flake8-builtins?

@kthy
Copy link
Author

kthy commented Feb 7, 2022

My example does an explicit definition of _ but that was just for repro purposes. The real use case is that the gettext module of the stdlib modifies the builtins on the fly, adding a _() function for translations.

we could add support in flake8-builtins to also warn if one overrides builtins provided via --builtins flake8 parameter/configuration. Would that be something you wish to have in flake8-builtins?

That's exactly what I was looking for, yes! 😁

@gforcada
Copy link
Owner

gforcada commented Oct 8, 2022

@kthy support for your specific request hasn't been implemented so far, but maybe #86 is a good enough solution for you?

Feel free to re-open if you disagree 😄

@gforcada gforcada closed this as completed Oct 8, 2022
@kthy
Copy link
Author

kthy commented Oct 9, 2022

I may be misunderstanding #86, but doesn't it do exactly the opposite of what I want here?

@gforcada
Copy link
Owner

gforcada commented Oct 9, 2022

Oh my, totally right 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants