Navigation Menu

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

__builtins__ and int are not defined #3867

Closed
diamond-lizard opened this issue Aug 24, 2017 · 9 comments
Closed

__builtins__ and int are not defined #3867

diamond-lizard opened this issue Aug 24, 2017 · 9 comments
Labels

Comments

@diamond-lizard
Copy link

When I run mypy against this test program ("foo.py"):

def gcd(a: int, b: int) -> int:
  while b:
    a, b = b, a % b
  return a

print(gcd( 'x', 15 ))

I get this error:

foo.py: error: Name '__builtins__' is not defined
foo.py:1: error: Name 'int' is not defined
foo.py:1: error: Cannot find module named 'builtins'
foo.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
foo.py:1: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.521
Traceback (most recent call last):
  File "/home/me/.local/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/main.py", line 97, in type_check_only
    options=options)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/build.py", line 196, in build
    graph = dispatch(sources, manager)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/build.py", line 1801, in dispatch
    process_graph(graph, manager)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/build.py", line 2044, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/build.py", line 2139, in process_stale_scc
    graph[id].semantic_analysis()
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/build.py", line 1696, in semantic_analysis
    self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/semanal.py", line 295, in visit_file
    self.accept(d)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/semanal.py", line 3645, in accept
    node.accept(self)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/nodes.py", line 565, in accept
    return visitor.visit_func_def(self)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/semanal.py", line 418, in visit_func_def
    self.analyze_function(defn)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/semanal.py", line 598, in analyze_function
    defn.type = self.type_analyzer().visit_callable_type(defn.type, nested=False)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/typeanal.py", line 333, in visit_callable_type
    fallback=t.fallback or self.named_type('builtins.function'),
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/typeanal.py", line 545, in named_type
    node = self.lookup_fqn_func(fully_qualified_name)
  File "/home/me/.local/lib64/python3.4/site-packages/mypy/semanal.py", line 3491, in lookup_fully_qualified
    n = self.modules[parts[0]]
KeyError: 'builtins'
foo.py:1: note: use --pdb to drop into pdb

I'm using python 3.4.5 under gentoo linux amd64. I had installed mypy using "pip3 install --user mypy" and my version of pip3 is "pip 9.0.1 from /usr/lib64/python3.4/site-packages (python 3.4)".

This was from a fresh install of mypy, but I then tried "pip3 install --user --upgrade mypy" which installed typing-3.6.2 but I still got the same error.

@ethanhs
Copy link
Collaborator

ethanhs commented Aug 24, 2017

Hi! This is usually a problem related to a broken installation. Could you paste (perhaps in a gist?) the output of calling mypy with the verbose flag -v (this should help us debug the issue). This will tell us where mypy is looking for typeshed, which it likely did not find correctly.

@ethanhs ethanhs added the crash label Aug 24, 2017
@gvanrossum
Copy link
Member

gvanrossum commented Aug 24, 2017 via email

@diamond-lizard
Copy link
Author

Here is the output of "mypy -v --show-traceback foo.py"

@gvanrossum
Copy link
Member

gvanrossum commented Aug 24, 2017 via email

@diamond-lizard
Copy link
Author

Apologies for the ignorant question, but how would I install the latest master?

I tried "pip3 install --user --upgrade mypy" and got:

Requirement already up-to-date: mypy in ./.local/lib64/python3.4/site-packages
Requirement already up-to-date: typing>=3.5.3; python_version < "3.5" in ./.local/lib64/python3.4/site-packages (from mypy)
Requirement already up-to-date: typed-ast<1.1.0,>=1.0.4 in ./.local/lib64/python3.4/site-packages (from mypy)

... and then got the same error when I ran mypy against foo.py

@diamond-lizard
Copy link
Author

diamond-lizard commented Aug 24, 2017

Nevermind. I figured it out:

git clone https://github.com/python/mypy.git
cd mypy
git submodule update --init typeshed
pip3 install --user --upgrade .

And now "mypy foo.py" seems to work:

foo.py:6: error: Argument 1 to "gcd" has incompatible type "str"; expected "int"

Thanks for your help!

@ethanhs
Copy link
Collaborator

ethanhs commented Aug 24, 2017

#3758 fixed this, it seems it was merged right around 0.521, and probably was missed.

@ethanhs ethanhs closed this as completed Aug 24, 2017
@ilevkivskyi
Copy link
Member

Nevermind. I figured it out:

git clone https://github.com/python/mypy.git
cd mypy
git submodule update --init typeshed
pip3 install --user --upgrade .

@gvanrossum @ethanhs
What do you think about an explicit recommendation in README to try master if one thinks there is a bug in PyPI version? Also the relevant section of the docs on installing from source is missing the typeshed submodule update step.

@gvanrossum
Copy link
Member

Oh, the "install from source" section definitely needs to be fixed.

But "try from master" is more easily done through a GitHub URL:

pip3 -U install git+https://github.com/python/mypy.git

It's probably a good idea to add a hint about trying that first if you think you've found a bug -- it's a common occurrence.

JukkaL pushed a commit that referenced this issue Oct 9, 2017
)

Update the installation from source instructions to also copy typeshed 
(see #3867). Also minor formatting improvements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants