Repository: python/mypy

Issue:
Can't use _ in typeshed
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]`


Discussion:
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.
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
We should probably do something about those official recommendations of monkey-patching; we don't go around recommending `from typing import *` either. :-)

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -4296,7 +4296,7 @@ def lookup(self, name: str, ctx: Context,
             assert isinstance(b.node, MypyFile)
             table = b.node.names
             if name in table:
-                if name[0] == "_" and name[1] != "_":
+                if len(name) > 1 and name[0] == "_" and name[1] != "_":
                     if not suppress_errors:
                         self.name_not_defined(name, ctx)
                     return None


Test patch: