Skip to content

Traceback when TypeVar variable name doesn't match name given and is used with isinstance #4962

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
TDHolmes opened this issue Apr 24, 2018 · 2 comments

Comments

@TDHolmes
Copy link

This occurs when the TypeVar name doesn't match AND we do an isinstance check on the argument. It is resolved when either of those are fixed/removed. (see repro code below)

  • Are you reporting a bug, or opening a feature request?
    bug

  • What are the versions of mypy and Python you are using?
    mypy version 0.590
    Python 3.6.5 (default, Apr 11 2018, 15:12:33)

  • What are the mypy flags you are using? (For example --strict-optional)
    --ignore-missing-imports --show-traceback

Traceback:

$ mypy ~/Desktop/mypy_repro.py --ignore-missing-imports --show-traceback
/Users/tylerholmes/Desktop/mypy_repro.py:4: error: String argument 1 'Point' to TypeVar(...) does not match variable name 'P'
/Users/tylerholmes/Desktop/mypy_repro.py:4: error: "object" not callable
/Users/tylerholmes/Desktop/mypy_repro.py:7: error: Invalid type "mypy_repro.P"
/Users/tylerholmes/Desktop/mypy_repro.py:8: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.590
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/usr/local/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/usr/local/lib/python3.6/site-packages/mypy/main.py", line 85, in main
    type_check_only(sources, bin_dir, options, flush_errors)
  File "/usr/local/lib/python3.6/site-packages/mypy/main.py", line 134, in type_check_only
    flush_errors=flush_errors)
  File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 183, in build
    flush_errors, fscache)
  File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 268, in _build
    graph = dispatch(sources, manager)
  File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2192, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2477, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2605, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1843, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 252, in check_first_pass
    self.accept(d)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 351, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.6/site-packages/mypy/nodes.py", line 542, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 607, in visit_func_def
    self._visit_func_def(defn)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 611, in _visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 671, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 834, in check_func_def
    self.accept(item.body)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 351, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.6/site-packages/mypy/nodes.py", line 807, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 1430, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 351, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.6/site-packages/mypy/nodes.py", line 974, in accept
    return visitor.visit_if_stmt(self)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 2294, in visit_if_stmt
    if_map, else_map = self.find_isinstance_check(e)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 2898, in find_isinstance_check
    return conditional_type_map(expr, vartype, type)
  File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 3320, in conditional_type_map
    remaining_type = restrict_subtype_away(current_type, proposed_precise_type)
  File "/usr/local/lib/python3.6/site-packages/mypy/subtypes.py", line 817, in restrict_subtype_away
    new_items = [item for item in t.relevant_items()
  File "/usr/local/lib/python3.6/site-packages/mypy/subtypes.py", line 818, in <listcomp>
    if (not (is_proper_subtype(erase_type(item), erased_s) or
  File "/usr/local/lib/python3.6/site-packages/mypy/erasetype.py", line 25, in erase_type
    return typ.accept(EraseTypeVisitor())
  File "/usr/local/lib/python3.6/site-packages/mypy/types.py", line 194, in accept
    return visitor.visit_unbound_type(self)
  File "/usr/local/lib/python3.6/site-packages/mypy/erasetype.py", line 31, in visit_unbound_type
    assert False, 'Not supported'
AssertionError: Not supported
/Users/tylerholmes/Desktop/mypy_repro.py:8: : note: use --pdb to drop into pdb

minimal code to reproduce:

from numbers import Real
from typing import Optional, Tuple, TypeVar, Union

P = TypeVar('Point', bound='Point')


def repro_func(name: str, value: Optional[Union[Real, P]]) -> Tuple[str, Real]:
    if isinstance(value, str):
        value = value[name]
    return name, value


repro_func("hi", None)
@TDHolmes
Copy link
Author

And we can't really rename the variable to Point because that collides with the class name Point that exists in this file.

@ilevkivskyi
Copy link
Member

The crash is fixed by #5258
Instead mypy gives a bunch of errors:

tstub.py:4: error: String argument 1 'Point' to TypeVar(...) does not match variable name 'P'
tstub.py:4: error: "object" not callable
tstub.py:7: error: Invalid type "tstub.P"
tstub.py:9: error: Invalid index type "str" for "str"; expected type "Union[int, slice]"
tstub.py:10: error: Incompatible return value type (got "Tuple[str, Union[Real, str, None]]", expected "Tuple[str, Real]")

Some of those are reasonable, but "object not callable" is clearly bogus. This is related to have special typing constructs are defined in typeshed. I will open an issue there instead.

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

2 participants