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

Internal Error with Union Type Involving Invalid Type #4653

Closed
ben-e-whitney opened this issue Feb 28, 2018 · 2 comments
Closed

Internal Error with Union Type Involving Invalid Type #4653

ben-e-whitney opened this issue Feb 28, 2018 · 2 comments

Comments

@ben-e-whitney
Copy link

A crash results when mypy --strict is run on the following.

import typing

import numpy as np

#This is the type of `np.ma.masked`.
MC = np.ma.core.MaskedConstant

class A:
    def __init__(self) -> None:
        self._error: typing.Union[float, MC, None] = None

    def error(self) -> float:
        if self._error is None:
            pass
        return 0.0

There is no crash when either of the following changes are made:

  1. I use the full type name instead of the alias in the annotation for self._error.
  2. I remove float and/or None from the union.

mypy complains that MC is an invalid type. I assume that is related. The traceback:

minimal.py:3: error: No library stub file for module 'numpy'
minimal.py:3: note: (Stub files are from https://github.com/python/typeshed)
minimal.py:10: error: Invalid type "minimal.MC"
minimal.py:13: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.560
Traceback (most recent call last):
  File "/usr/bin/mypy", line 11, in <module>
    load_entry_point('mypy==0.560', 'console_scripts', 'mypy')()
  File "/usr/lib/python3/dist-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/usr/lib/python3/dist-packages/mypy/main.py", line 66, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/lib/python3/dist-packages/mypy/main.py", line 119, in type_check_only
    options=options)
  File "/usr/lib/python3/dist-packages/mypy/build.py", line 218, in build
    graph = dispatch(sources, manager)
  File "/usr/lib/python3/dist-packages/mypy/build.py", line 1997, in dispatch
    process_graph(graph, manager)
  File "/usr/lib/python3/dist-packages/mypy/build.py", line 2299, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/lib/python3/dist-packages/mypy/build.py", line 2475, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/lib/python3/dist-packages/mypy/build.py", line 1876, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 196, in check_first_pass
    self.accept(d)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 286, in accept
    stmt.accept(self)
  File "/usr/lib/python3/dist-packages/mypy/nodes.py", line 696, in accept
    return visitor.visit_class_def(self)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 1210, in visit_class_def
    self.accept(defn.defs)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 286, in accept
    stmt.accept(self)
  File "/usr/lib/python3/dist-packages/mypy/nodes.py", line 756, in accept
    return visitor.visit_block(self)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 1330, in visit_block
    self.accept(s)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 286, in accept
    stmt.accept(self)
  File "/usr/lib/python3/dist-packages/mypy/nodes.py", line 497, in accept
    return visitor.visit_func_def(self)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 534, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 599, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 762, in check_func_def
    self.accept(item.body)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 286, in accept
    stmt.accept(self)
  File "/usr/lib/python3/dist-packages/mypy/nodes.py", line 756, in accept
    return visitor.visit_block(self)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 1330, in visit_block
    self.accept(s)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 286, in accept
    stmt.accept(self)
  File "/usr/lib/python3/dist-packages/mypy/nodes.py", line 921, in accept
    return visitor.visit_if_stmt(self)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 2191, in visit_if_stmt
    if_map, else_map = self.find_isinstance_check(e)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 2812, in find_isinstance_check
    return find_isinstance_check(n, self.type_map)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 3140, in find_isinstance_check
    if_vars, else_vars = conditional_type_map(expr, vartype, none_typ)
  File "/usr/lib/python3/dist-packages/mypy/checker.py", line 2877, in conditional_type_map
    remaining_type = restrict_subtype_away(current_type, proposed_precise_type)
  File "/usr/lib/python3/dist-packages/mypy/subtypes.py", line 784, in restrict_subtype_away
    new_items = [item for item in t.relevant_items()
  File "/usr/lib/python3/dist-packages/mypy/subtypes.py", line 785, in <listcomp>
    if (not (is_proper_subtype(erase_type(item), erased_s) or
  File "/usr/lib/python3/dist-packages/mypy/erasetype.py", line 25, in erase_type
    return typ.accept(EraseTypeVisitor())
  File "/usr/lib/python3/dist-packages/mypy/types.py", line 194, in accept
    return visitor.visit_unbound_type(self)
  File "/usr/lib/python3/dist-packages/mypy/erasetype.py", line 31, in visit_unbound_type
    assert False, 'Not supported'
AssertionError: Not supported
minimal.py:13: : note: use --pdb to drop into pdb

Version numbers:

$ mypy --version
pymypy 0.560
$ python3 --version
Python 3.6.4+
@gvanrossum
Copy link
Member

There are no numpy stubs, so that's presumably why MC is deemed invalid. The crash itself is probably due to #4543, but I'm not sure whether to call it a duplicate (since that issue has a forward reference, not an invalid type).

@ilevkivskyi
Copy link
Member

Yes, this is a duplicate of #4543, so I am closing this.

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

No branches or pull requests

3 participants