Skip to content

AttributeError: 'NoneType' object has no attribute 'accept' #2440

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
michaelbarton opened this issue Nov 11, 2016 · 12 comments
Closed

AttributeError: 'NoneType' object has no attribute 'accept' #2440

michaelbarton opened this issue Nov 11, 2016 · 12 comments

Comments

@michaelbarton
Copy link

michaelbarton commented Nov 11, 2016

I've searched the issues before posting this, #1382 has the same title but the
problem described there doesn't seem to be the same issue that I am having. The
problem in #1382 appears to be related to an external library
colorama/win32.py which I am not using.

I've tried creating a types file for my python project:

from typing import List, Dict, Set, Optional, Union, NamedTuple, Tuple
from enum   import Enum

class GeneType(Enum):
    CDS, rRNA, tRNA = range(3)

Gene = NamedTuple('Gene',
        [('ID', str), ('GeneType', GeneType), ('Product', str)])

AssemblyMetricSet = Tuple[str, Dict[str, int]]

def generate_gene_set_metrics(genes: List[Gene]) -> AssemblyMetricSet:
    return ("", {"", 1})

When using the mypy command to check this:

mypy --show-traceback --disallow-untyped-defs --strict-optional --silent-imports -p gaet

I get the traceback below:

tox --
GLOB sdist-make: /Users/user/cache/prokka/setup.py
py35-unit inst-nodeps: /Users/user/cache/prokka/.tox/dist/gaet-0.1.0.zip
py35-unit installed: funcy==1.7.1,gaet==0.1.0,mypy-lang==0.4.5,py==1.4.31,pytest==3.0.4,ruamel.yaml==0.11.15
py35-unit runtests: PYTHONHASHSEED='1'
py35-unit runtests: commands[0] | mypy --show-traceback --disallow-untyped-defs --strict-optional --silent-imports -p gaet
Traceback (most recent call last):
  File ".tox/py35-unit/bin/mypy", line 6, in <module>
    main(__file__)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/main.py", line 38, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/main.py", line 79, in type_check_only
    options=options)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 181, in build
    dispatch(sources, manager)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1470, in dispatch
    process_graph(graph, manager)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1650, in process_graph
    process_stale_scc(graph, scc)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1723, in process_stale_scc
    graph[id].semantic_analysis_pass_three()
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1406, in semantic_analysis_pass_three
    self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath, self.options)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2770, in visit_file
    self.accept(file_node)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2774, in accept
    node.accept(self)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/nodes.py", line 216, in accept
    return visitor.visit_mypy_file(self)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/traverser.py", line 29, in visit_mypy_file
    d.accept(self)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/nodes.py", line 507, in accept
    return visitor.visit_func_def(self)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2785, in visit_func_def
    self.analyze(fdef.type)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2871, in analyze
    type.accept(analyzer)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/types.py", line 639, in accept
    return visitor.visit_callable_type(self)
  File "/Users/user/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/typeanal.py", line 379, in visit_callable_type
    t.ret_type.accept(self)
AttributeError: 'NoneType' object has no attribute 'accept'
ERROR: InvocationError: '/Users/user/cache/prokka/.tox/py35-unit/bin/mypy --show-traceback --disallow-untyped-defs --strict-optional --silent-imports -p gaet'
___________________________________ summary ____________________________________
ERROR:   py35-unit: commands failed

@michaelbarton
Copy link
Author

I've editted my code sample, it appears the bug is produced when a function is also defined with the type.

@refi64
Copy link
Contributor

refi64 commented Nov 11, 2016

What's the definition of AssemblyMetricSet?

@michaelbarton
Copy link
Author

The following code does not produce an error:

def generate_gene_set_metrics(genes: List[Gene]) -> Dict[str, int]:
    return {}

The following code does:

def generate_gene_set_metrics(genes: List[Gene]) -> Dict[str, int]:
    return {"", 1}

@michaelbarton
Copy link
Author

Sorry I renamed ReferenceMetricSet in the mean time, it is:

AssemblyMetricSet = Dict[str, int]

@michaelbarton
Copy link
Author

I've updated the original code sample

@refi64
Copy link
Contributor

refi64 commented Nov 11, 2016

Just tried it, but it's working normally:

ryan@DevPC-LX /media/ryan/stuff/mypy master $ cat z.py
from typing import List, Dict, Set, Optional, Union, NamedTuple, Tuple
from enum   import Enum

class GeneType(Enum):
    CDS, rRNA, tRNA = range(3)

Gene = NamedTuple('Gene',
        [('ID', str), ('GeneType', GeneType), ('Product', str)])

AssemblyMetricSet = Tuple[str, Dict[str, int]]

def generate_gene_set_metrics(genes: List[Gene]) -> AssemblyMetricSet:
    return ("", {"", 1})
ryan@DevPC-LX /media/ryan/stuff/mypy master $ mypy --show-traceback --disallow-untyped-defs --stri
............................................. ct-optional --silent-imports z.py
z.py: note: In function "generate_gene_set_metrics":
z.py:13: error: Incompatible return value type (got "Tuple[str, Set[object]]", expected "Tuple[str, Dict[str, int]]")
ryan@DevPC-LX /media/ryan/stuff/mypy master $ 

@michaelbarton
Copy link
Author

michaelbarton commented Nov 11, 2016

This seems to be how I can replicate this in my code:

gaet/types.py

from typing import List, Dict, Set, Optional, Union, NamedTuple, Tuple
from enum   import Enum

AssemblyMetricSet = Tuple[str, Dict[str, int]]

gaet/gene_set.py


def f_1() -> List[Tuple[str, int]]:
    return []


def f_2() -> List[Tuple[str, int]]:
    return [("", 1)]


def f_3() -> Dict[str, int]:
    return {}


def f_4() -> Dict[str, int]:
    return {"key": 1}


def f_5() -> AssemblyMetricSet:
    return ("", {"key": 1})


def f_6() -> AssemblyMetricSet:
    return ("", {"key", 1})

However I get the expected type error instead of the error in the title if I add

AssemblyMetricSet = Tuple[str, Dict[str, int]]

To the gaet/gene_set.py file. This suggest to me that it might be something to
do with imports? I am by no means an expert, I am very new to python, but I am
enjoying the type system so far.

@refi64
Copy link
Contributor

refi64 commented Nov 11, 2016

@michaelbarton

ryan@DevPC-LX /media/ryan/stuff/mypy master $ mypy --show-traceback --disallow-untyped-defs --stri
............................................. ct-optional --silent-imports -p gaet
gaet/gaet_set.py: note: In function "f_1":
gaet/gaet_set.py:1: error: Name 'Tuple' is not defined
gaet/gaet_set.py: note: In function "f_2":
gaet/gaet_set.py:5: error: Name 'Tuple' is not defined
gaet/gaet_set.py: note: In function "f_5":
gaet/gaet_set.py:17: error: Name 'AssemblyMetricSet' is not defined
gaet/gaet_set.py: note: In function "f_6":
gaet/gaet_set.py:21: error: Name 'AssemblyMetricSet' is not defined
ryan@DevPC-LX /media/ryan/stuff/mypy master $ 

@michaelbarton
Copy link
Author

example/types.py

from typing import Dict, Tuple

TestType = Tuple[str, Dict[str, int]]

example/functions.py

from example.types import *

def f() -> TestType:
    return ("name", {"key", 1})

command:

mypy --show-traceback --disallow-untyped-defs --strict-optional --silent-imports -p example >| bug.txt

bug.txt

Traceback (most recent call last):
  File "prokka/.tox/py35-unit/bin/mypy", line 6, in <module>
    main(__file__)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/main.py", line 38, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/main.py", line 79, in type_check_only
    options=options)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 181, in build
    dispatch(sources, manager)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1470, in dispatch
    process_graph(graph, manager)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1650, in process_graph
    process_stale_scc(graph, scc)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1723, in process_stale_scc
    graph[id].semantic_analysis_pass_three()
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/build.py", line 1406, in semantic_analysis_pass_three
    self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath, self.options)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2770, in visit_file
    self.accept(file_node)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2774, in accept
    node.accept(self)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/nodes.py", line 216, in accept
    return visitor.visit_mypy_file(self)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/traverser.py", line 29, in visit_mypy_file
    d.accept(self)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/nodes.py", line 507, in accept
    return visitor.visit_func_def(self)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2785, in visit_func_def
    self.analyze(fdef.type)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/semanal.py", line 2871, in analyze
    type.accept(analyzer)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/types.py", line 639, in accept
    return visitor.visit_callable_type(self)
  File "/Users/michaelbarton/cache/prokka/.tox/py35-unit/lib/python3.5/site-packages/mypy/typeanal.py", line 379, in visit_callable_type
    t.ret_type.accept(self)
AttributeError: 'NoneType' object has no attribute 'accept'

@refi64
Copy link
Contributor

refi64 commented Nov 11, 2016

...sorry, I think this is a dup of #2315.

@michaelbarton
Copy link
Author

No worries. Thanks for looking into it. :)

@gvanrossum
Copy link
Member

Thanks Ryan for triaging 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