Skip to content

Can't use _ in typeshed #9802

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 Dec 13, 2020 · 3 comments · Fixed by #11811
Closed

Can't use _ in typeshed #9802

Dreamsorcerer opened this issue Dec 13, 2020 · 3 comments · Fixed by #11811
Labels

Comments

@Dreamsorcerer
Copy link
Contributor

As has been recommended as a workaround, I've tried using a custom typeshed directory to modify the builtins that Mypy sees, so that it recognises the _() function.

When adding to builtins.pyi:

from gettext import gettext
_ = gettext

I get:

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.790
Traceback (most recent call last):
  File "mypy/semanal.py", line 4700, in accept
  File "mypy/nodes.py", line 1141, in accept
  File "mypy/semanal.py", line 3186, in visit_return_stmt
  File "mypy/nodes.py", line 1545, in accept
  File "mypy/semanal.py", line 3537, in visit_call_expr
  File "mypy/nodes.py", line 1545, in accept
  File "mypy/semanal.py", line 3454, in visit_call_expr
  File "mypy/nodes.py", line 1477, in accept
  File "mypy/semanal.py", line 3382, in visit_name_expr
  File "mypy/semanal.py", line 3895, in lookup
IndexError: string index out of range

Also, when I try:

from gettext import gettext as _

I don't get a crash, but Mypy still doesn't recognise it as a global function. Still producing dozens of:
error: Name '_' is not defined [name-defined]

@Dreamsorcerer Dreamsorcerer changed the title Crash when using _ in typeshed Can't use _ in typeshed Dec 13, 2020
@gvanrossum
Copy link
Member

The problem is likely due to circular references -- clearly gettext.pyi depends on builtins.pyi (since every module does), so adding a dependency on gettext.pyi to builtins.pyi causes problems.

I'm sure you have already considered and rejected putting the import at the top of your own source files. I recommend reconsidering that, at least if you want static type checking to work for your code.

@Dreamsorcerer
Copy link
Contributor Author

Actually, it works if I change it to:

from gettext import gettext
underscore = gettext

Still doesn't do anything with from gettext import gettext as underscore though.

So, seems that there may be several parts here:

  • _ alone causes a crash
  • names prefixed with _ don't appear (maybe Mypy sees them as private variables and doesn't export them even in builtins?).
  • import as doesn't seem to work in builtins.

I could reconsider importing the function, I was just following what seemed to be recommended in the docs, e.g.:

For convenience, you want the _() function to be installed in Python’s builtins namespace, so it is easily accessible in all modules of your application.

https://docs.python.org/3/library/gettext.html#gettext.install
https://docs.python.org/3/library/gettext.html#localizing-your-application

@gvanrossum
Copy link
Member

We should probably do something about those official recommendations of monkey-patching; we don't go around recommending from typing import * either. :-)

StefanM-TT added a commit to StefanM-TT/mypy that referenced this issue Dec 21, 2021
allows the definiton of "_" as function in builtins (e.g via typeshed).

fixes issue python#9802
hauntsaninja pushed a commit that referenced this issue Dec 21, 2021
allows the definition of "_" as function in builtins (e.g via typeshed).

fixes #9802
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this issue Jan 20, 2022
allows the definition of "_" as function in builtins (e.g via typeshed).

fixes python#9802
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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants