Skip to content

Getting "Cannot access member" error when inside a hasattr check  #2237

@kimadeline

Description

@kimadeline

Environment data

  • Language Server version: 2022.1.0 (pyright 5829dcbb)
  • OS and version: macOS Big Sur 11.6.1
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8 venv

Expected behaviour

When using if hasattr to check if an attribute is present, I shouldn't get a "Cannot access member" error for code inside that condition.

Actual behaviour

I get the following error:

Cannot access member "decorator_list" for type "stmt"
  Member "decorator_list" is unknown (Pylance) reportGeneralTypeIssues

Logs

Copy-pasting the logs froze the window, so I am not going to include them. Let me know if you want them and I'll send them your way 🙂

Code Snippet / Additional information

import ast
import sys

tree = ast.parse("print('Hello World')")

for node in tree.body[1:]:
   line_end = node.lineno - 1
   if hasattr(node, "decorator_list") and sys.version_info.major >= 3:
      line_end -= len(node.decorator_list)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions