Skip to content

AttributeError: 'UnionType' object has no attribute 'type' #3468

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
zero323 opened this issue May 26, 2017 · 17 comments · Fixed by #4017
Closed

AttributeError: 'UnionType' object has no attribute 'type' #3468

zero323 opened this issue May 26, 2017 · 17 comments · Fixed by #4017

Comments

@zero323
Copy link

zero323 commented May 26, 2017

This looks like a regression (?) between Mypy 0.501 and 0.510.

Failure is reported at this line: https://github.com/zero323/pyspark-stubs/blob/d25da4d051f878d358bdef6a0e08a6597afbee4f/third_party/3/pyspark/sql/functions.pyi#L24 but it can be reproduced with simplified input:

# Stubs for pyspark.sql.functions (Python 3.5)

from typing import Union
from pyspark.sql.column import Column

ColumnOrName = Union[pyspark.sql.column.Column, str]

def last(col: ColumnOrName, ignorenulls: bool = ...) -> Column: ...

Python version:

python --version
Python 3.5.3 :: Anaconda custom (64-bit)

Mypy version: 0.510, 0.511

Traceback:

third_party/3/pyspark/sql/column.pyi:24: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.511
Traceback (most recent call last):
  File "/home/user/anaconda3/bin/mypy", line 6, in <module>
    main(__file__)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/main.py", line 46, in main
    res = type_check_only(sources, bin_dir, options)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/main.py", line 93, in type_check_only
    options=options)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/build.py", line 188, in build
    graph = dispatch(sources, manager)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/build.py", line 1595, in dispatch
    process_graph(graph, manager)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/build.py", line 1838, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/build.py", line 1937, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/build.py", line 1510, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 177, in check_first_pass
    self.accept(d)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 264, in accept
    stmt.accept(self)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/nodes.py", line 750, in accept
    return visitor.visit_class_def(self)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 1080, in visit_class_def
    self.accept(defn.defs)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 264, in accept
    stmt.accept(self)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/nodes.py", line 811, in accept
    return visitor.visit_block(self)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 1173, in visit_block
    self.accept(s)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 264, in accept
    stmt.accept(self)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/nodes.py", line 565, in accept
    return visitor.visit_func_def(self)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 510, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 569, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 616, in check_func_def
    self.check_reverse_op_method(item, typ, name)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 806, in check_reverse_op_method
    defn)
  File "/home/user/anaconda3/lib/python3.5/site-packages/mypy/checker.py", line 876, in check_overlapping_op_methods
    forward_base.type.name(), forward_name, context)
AttributeError: 'UnionType' object has no attribute 'type'
third_party/3/pyspark/sql/column.pyi:24: note: use --pdb to drop into pdb
@gvanrossum
Copy link
Member

Can you come up with a repro that doesn't still depend on the pyspark stubs? I get an error on the import line.

@zero323
Copy link
Author

zero323 commented May 26, 2017

Let me try to dig deeper into this over the weekend. It doesn't appear in isolation, so it guess the problem can be somewhere completely else, and just manifests in an awkward way.

@ilevkivskyi
Copy link
Member

Here is a simple repro for the crash:

class X:
    def __mod__(self, other: Union[X, str]) -> X: ...
    def __rmod__(self, other: Union[X, str]) -> X: ...

I think the problem is because of an unsafe cast here https://github.com/python/mypy/blob/master/mypy/checker.py#L805

@ilevkivskyi ilevkivskyi added bug mypy got something wrong crash priority-1-normal labels May 28, 2017
@gvanrossum gvanrossum removed the bug mypy got something wrong label May 28, 2017
@gvanrossum
Copy link
Member

Ah, casts! They cause so much trouble and are so misunderstood. That one should have been an assert, but how to prevent crashing?

(PS Crash >= Bug so I removed the bug label.)

@ilevkivskyi
Copy link
Member

but how to prevent crashing?

TBH I am not sure what to do here. It looks like this crash happens only if Any appears in the special method signature (like in str.__mod__). @JukkaL do you have any ideas?

@JukkaL
Copy link
Collaborator

JukkaL commented May 30, 2017

I can have a look at this. The relevant code is really old and seems to have bit rotted (assuming that it ever worked).

@JukkaL JukkaL self-assigned this May 30, 2017
@elazarg
Copy link
Contributor

elazarg commented Jun 4, 2017

I think it is related to the issue I have described in #3227

@wilfreddenton
Copy link

wilfreddenton commented Nov 28, 2017

I get the same error when I run mypy on fractions.pyi in the typeshed. Using mypy 0.550.

❯ mypy /Users/wilfred/python-envs/sailfish/lib/mypy/typeshed/stdlib/2and3/fractions.pyi --show-traceback
/Users/wilfred/python-envs/sailfish/lib/mypy/typeshed/stdlib/2and3/fractions.pyi:80: error: INTERNAL ERROR -- please report a bug at https://github.com/pyth
on/mypy/issues version: 0.550                                                                                                                              
Traceback (most recent call last):
  File "/Users/wilfred/python-envs/sailfish/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/main.py", line 66, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/main.py", line 119, in type_check_only
    options=options)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/build.py", line 214, in build
    graph = dispatch(sources, manager)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/build.py", line 1935, in dispatch
    process_graph(graph, manager)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/build.py", line 2199, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/build.py", line 2327, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/build.py", line 1836, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 196, in check_first_pass
    self.accept(d)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 283, in accept
    stmt.accept(self)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/nodes.py", line 690, in accept
    return visitor.visit_class_def(self)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 1185, in visit_class_def
    self.accept(defn.defs)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 283, in accept
    stmt.accept(self)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/nodes.py", line 750, in accept
    return visitor.visit_block(self)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 1305, in visit_block
    self.accept(s)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 283, in accept
    stmt.accept(self)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/nodes.py", line 496, in accept
    return visitor.visit_func_def(self)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 531, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 591, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 641, in check_func_def
    self.check_reverse_op_method(item, typ, name)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 859, in check_reverse_op_method
    defn)
  File "/Users/wilfred/python-envs/sailfish/lib/python3.5/site-packages/mypy/checker.py", line 929, in check_overlapping_op_methods
    forward_base.type.name(), forward_name, context)
AttributeError: 'UnionType' object has no attribute 'type'
/Users/wilfred/python-envs/sailfish/lib/mypy/typeshed/stdlib/2and3/fractions.pyi:80: : note: use --pdb to drop into pdb

However if I run the same command but within my home directory I do not have this issue. I made sure to delete all instances of .mypy_cache.

@elazarg
Copy link
Contributor

elazarg commented Nov 28, 2017

I can't reproduce this, but there's a slim chance that #4017 will avoid the crash.

@elazarg
Copy link
Contributor

elazarg commented Nov 28, 2017

@ilevkivskyi in your repro (with the additional from typing import Union), #4017 gives

tmp.py:5: error: Signatures of "__rmod__" of "X" and "__mod__" of "Union[X, str]" are unsafely overlapping

I'm not sure if that's correct, but at least it's not a crash.

@wilfreddenton
Copy link

I just created a new env and then the errors went away. Not sure what why. I had deleted all instances of .mypy_cache and __pycache__ in the old env before running mypy.

@ilevkivskyi
Copy link
Member

@elazarg I am not sure this is correct behaviour, IIUC this error is intended for cases where an argument is a superclass (because of some Python logic that binary methods are called on the most derived class), therefore it should probably only apply to Instances. But this code is very old, so @JukkaL knows much more about this.

@ilevkivskyi
Copy link
Member

The invalid cast I mentioned moved to another line https://github.com/python/mypy/blob/master/mypy/checker.py#L925

@elias6
Copy link
Contributor

elias6 commented Feb 18, 2018

Link to the invalid cast that will not go out of date if the content of checker.py changes:

typ2, other_method, cast(Instance, arg_type),

@elazarg
Copy link
Contributor

elazarg commented Feb 18, 2018

I'm not sure the specific invalid cast is the important thing - either #4017 solve it (in which case I believe it should be merged), or the problem is not the cast itself.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 19, 2018

Increased priority as the crash has been reported multiple times.

JukkaL pushed a commit that referenced this issue Mar 1, 2018
* Remove old comments, clean bail-out logic
* Use fallback to find Instance when possible
* Remove cast; move formatting logic to messages.py
* Remove unused method
* Rename variables to match check_overlapping_op_methods terminology
* Remove many unused imports

Potential fix to #3468, or at least avoids the crash (since it removes the unsafe cast).
@JukkaL
Copy link
Collaborator

JukkaL commented Mar 1, 2018

#4017 hopefully fixed this. Let's reopen if this continues to be a problem.

yedpodtrzitko pushed a commit to kiwicom/mypy that referenced this issue Mar 15, 2018
* Remove old comments, clean bail-out logic
* Use fallback to find Instance when possible
* Remove cast; move formatting logic to messages.py
* Remove unused method
* Rename variables to match check_overlapping_op_methods terminology
* Remove many unused imports

Potential fix to python#3468, or at least avoids the crash (since it removes the unsafe cast).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants