Skip to content

Name '_' is not defined with gettext.install() #8727

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

Closed
Dreamsorcerer opened this issue Apr 26, 2020 · 18 comments
Closed

Name '_' is not defined with gettext.install() #8727

Dreamsorcerer opened this issue Apr 26, 2020 · 18 comments

Comments

@Dreamsorcerer
Copy link
Contributor

I get Name '_' is not defined after using gettext.install(). Is there some configuration option to specify this is a builtin?

@msullivan
Copy link
Collaborator

I don't think so. It might be possible to do with a plugin?

@JukkaL
Copy link
Collaborator

JukkaL commented May 1, 2020

Modifying the stubs of builtins in typeshed locally and using --custom-typeshed-dir is perhaps the easiest way to make this work.

Better support for monkeypatching stdlib might be a reasonable feature to have at some point, if we can implement it in a simple enough way. One option would to write a generic plugin for this.

@Dreamsorcerer
Copy link
Contributor Author

Is there any way to atleast ignore any name-defined errors globally? Valid errors are caught by basic flake8 linting, so I don't think mypy is actually adding anything for me with these error codes.

@aivarannamaa
Copy link

Pylint has additional-builtins configuration option. It would be great if MyPy had something like this.

@Dreamsorcerer
Copy link
Contributor Author

I can spend a full week working on Mypy shortly, is this something I'm likely to be able to do? Is adding a config option to extend the builtins an acceptable solution? If so, can you point me to the code I would need to look at. Thanks.

@nicoddemus
Copy link

Is adding a config option to extend the builtins an acceptable solution?

Can't speak for the maintainers, but an option that allows me to point to an .pyi file with "extra builtins" would solve this problem for me.

@JelleZijlstra
Copy link
Member

A way that would work right now is to fork typeshed to add _ to builtins, then pass --custom-typeshed-dir to your mypy invocation. Doing it without --custom-typeshed-dir is considerably harder; I'm guessing you'll have to find where mypy internally retrieves the builtins stub and inject your new builtin.

@JelleZijlstra
Copy link
Member

(Also, I'm not sure a PR adding the ability to inject builtins would be accepted.)

@nicoddemus
Copy link

A way that would work right now is to fork typeshed to add _ to builtins, then pass --custom-typeshed-dir to your mypy invocation.

Indeed that's what I did and I can confirm it works.

(Also, I'm not sure a PR adding the ability to inject builtins would be accepted.)

Indeed.

@Dreamsorcerer
Copy link
Contributor Author

A way that would work right now is to fork typeshed to add _ to builtins, then pass --custom-typeshed-dir to your mypy invocation.

But, that requires cloning and maintaining the typeshed repo, right? That doesn't feel like the right solution to close this ticket. I'm expecting something that can extend Mypy's behaviour without requiring that extra overhead.

e.g. Following the suggestion to have a .pyi file that can be used to extend the builtiins; this would only require maintaining a 1 line file to support my example use case.

@JelleZijlstra
Copy link
Member

But, that requires cloning and maintaining the typeshed repo, right? That doesn't feel like the right solution to close this ticket. I'm expecting something that can extend Mypy's behaviour without requiring that extra overhead.

Yes, it definitely has some overhead, but on the plus side it doesn't require deep changes to mypy.

Another hack that I think would work is to copy just builtins.pyi from typeshed, add your custom globals, and put it somewhere in your MYPYPATH. That means you have to maintain a fork of just one file instead of all of mypy.

@Dreamsorcerer
Copy link
Contributor Author

Dreamsorcerer commented Oct 10, 2020

Seems the latter is not an option:

mypy: "stubs/builtins.pyi" shadows library module "builtins"
note: A user-defined top-level module with name "builtins" is not supported

@nicoddemus
Copy link

nicoddemus commented Dec 21, 2020

Another hack that I think would work is to copy just builtins.pyi from typeshed, add your custom globals, and put it somewhere in your MYPYPATH. That means you have to maintain a fork of just one file instead of all of mypy.

That unfortunately does not work it seems:

mypy: "d:\myproject\stubs\builtins.pyi" shadows library module "builtins"
note: A user-defined top-level module with name "builtins" is not supported

@huyz
Copy link

huyz commented Apr 4, 2021

Another package that install()s into builtins and for which a simple solution would be useful: https://github.com/gruns/icecream

@matthewrv
Copy link

A way that would work right now is to fork typeshed to add _ to builtins, then pass --custom-typeshed-dir to your mypy invocation.

Indeed that's what I did and I can confirm it works.

(Also, I'm not sure a PR adding the ability to inject builtins would be accepted.)

Indeed.

Sorry, maybe I misunderstood something. I tried to fork typeshed, modify builtins.pyi and add it to mypy but i got error with this traceback. Am I doing something wrong?

(.venv) D:\>mypy test.py --custom-typeshed-dir D:\my_project\typeshed --show-traceback
test.py:1: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.812
Traceback (most recent call last):
  File "C:\Users\matthewrv\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\matthewrv\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "mypy\semanal.py", line 4835, in accept
  File "mypy\nodes.py", line 1030, in accept
  File "mypy\semanal.py", line 3306, in visit_expression_stmt
  File "mypy\nodes.py", line 1555, in accept
  File "mypy\semanal.py", line 3581, in visit_call_expr
  File "mypy\nodes.py", line 1487, in accept
  File "mypy\semanal.py", line 3509, in visit_name_expr
  File "mypy\semanal.py", line 4023, in lookup
IndexError: string index out of range
test.py:1: : note: use --pdb to drop into pdb

As I undestrand this happens here:

mypy/mypy/semanal.py

Lines 4023 to 4025 in d089891

if name[0] == "_" and name[1] != "_":
if not suppress_errors:
self.name_not_defined(name, ctx)

@Dreamsorcerer
Copy link
Contributor Author

See #9802

The person you quoted was using another function (which does not start with _).

@Rosadosa
Copy link

Rosadosa commented Aug 4, 2021

Any update on implementing a way to ignore the _() that gets installed by gettext?
Would be great to have a builtin tags like flake8 and pylint have!

@Dreamsorcerer
Copy link
Contributor Author

My impression from various comments is that this practice is discouraged (even if the gettext documentation has not been updated to say this...), and it's unlikely to be supported any time soon (if ever).

I'm fine with closing this issue as wont-fix if any of the mypy developers want to confirm that is the case, rather than just leaving it open as a vague hope that it might happen.

israellevin pushed a commit to israellevin/mcomix that referenced this issue Mar 31, 2024
Monkeypatching builtins to "install" the _() function is frowned upon,
and unsupported by mypy[1]. It's also unnecessary, as it's perfectly
workable to create a _() function definition and import it where needed.
This commit adds `from mcomix.i18n import _` to the imports of all
modules that use translations, replacing the monkeypatched global.
[1]: python/mypy#8727
RoDuth added a commit to RoDuth/ghini.desktop that referenced this issue May 18, 2024
Fixes ~300 mypy errors and is highly discouraged, see:
python/mypy#8727
python/mypy#9802
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants