Skip to content

AttributeError: 'AnyType' object has no attribute 'type' in try/except/else #1948

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
tharvik opened this issue Jul 27, 2016 · 1 comment
Closed

Comments

@tharvik
Copy link
Contributor

tharvik commented Jul 27, 2016

def f(): ...  # just to have something returning Any

def lookup_field(name, obj):
    try:
        pass
    except:
        attr = f()
    else:
        attr = None

with --check-untyped-defs --strict-optional gives

Traceback (most recent call last):
  File "/usr/lib/python-exec/python3.4/mypy", line 6, in <module>
    main(__file__)
  File "/usr/lib64/python3.4/site-packages/mypy/main.py", line 40, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/lib64/python3.4/site-packages/mypy/main.py", line 81, in type_check_only
    options=options)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 178, in build
    dispatch(sources, manager)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1325, in dispatch
    process_graph(graph, manager)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1468, in process_graph
    process_stale_scc(graph, scc)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1545, in process_stale_scc
    graph[id].type_check()
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1303, in type_check
    manager.type_checker.visit_file(self.tree, self.xpath)
  File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 152, in visit_file
    self.accept(d)
  File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 201, in accept
    typ = node.accept(self)
  File "/usr/lib64/python3.4/site-packages/mypy/nodes.py", line 476, in accept
    return visitor.visit_func_def(self)
  File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 399, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 466, in check_func_item
    self.leave_partial_types()
  File "/usr/lib64/python3.4/site-packages/mypy/checker.py", line 2191, in leave_partial_types
    if experiments.STRICT_OPTIONAL and cast(PartialType, var.type).type is None:
AttributeError: 'AnyType' object has no attribute 'type'

*** INTERNAL ERROR ***

/tmp/asd8.py:3: error: Internal error -- please report a bug at https://github.com/python/mypy/issues

NOTE: you can use "mypy --pdb ..." to drop into the debugger when this happens.

This is a small repro based on django:django/contrib/admin/utils.py:276.

@tharvik tharvik changed the title AttributeError: 'AnyType' object has no attribute 'type' in try/except and AttributeError: 'AnyType' object has no attribute 'type' in try/except/else Jul 27, 2016
@gnprice
Copy link
Collaborator

gnprice commented Aug 4, 2016

Thanks for reporting this! Definitely a bug in --strict-optional.

It looks like the cast is mistaken -- var.type is actually an AnyType, not a PartialType.

@gnprice gnprice added this to the 0.4.x milestone Aug 4, 2016
gvanrossum pushed a commit that referenced this issue Aug 5, 2016

Some --strict-optional code previously assumed that the partial_types list could only contain PartialTypes. In some cases, it appears AnyType can also be part of that list. It's not entirely clear to me what causes this, but it looks like the correct solution is to be robust to the presence of other types in that list.

Fixes #1948.
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

4 participants