Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Reset counters only in existing class def scopes #50

Merged
merged 1 commit into from Mar 28, 2023

Conversation

gdrosos
Copy link
Collaborator

@gdrosos gdrosos commented Mar 22, 2023

Description

When PyCG processes a new class definition, it tries to reset the counters of the scope within the namespace of the correspondoing class. In some cases, were a scope object has not been instantiated for a given namespace, the reset_counters() method is called upon a NoneType Object resulting in an AttributeError which breaks PyCG.
We adopted the visit_ClassDef method to reset the counter of a corresponding namespace scope only when the specific scope exists.

Steps to Test

Before this PR, having downloaded and unziped the neuromation:20.12.7 pypi library, when running the

pycg --package neuromation $(find neuromation -type f -name "*.py") --fasten
we used to get the following error

   ...
    File "/opt/homebrew/lib/python3.10/site-packages/pycg-0.0.6-py3.10.egg/pycg/processing/preprocessor.py", line 65, in analyze_submodule
    super().analyze_submodule(PreProcessor, modname,
  File "/opt/homebrew/lib/python3.10/site-packages/pycg-0.0.6-py3.10.egg/pycg/processing/base.py", line 485, in analyze_submodule
    visitor.analyze()
  File "/opt/homebrew/lib/python3.10/site-packages/pycg-0.0.6-py3.10.egg/pycg/processing/preprocessor.py", line 375, in analyze
    self.visit(ast.parse(self.contents, self.filename))
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/opt/homebrew/lib/python3.10/site-packages/pycg-0.0.6-py3.10.egg/pycg/processing/preprocessor.py", line 114, in visit_Module
    super().visit_Module(node)
  File "/opt/homebrew/lib/python3.10/site-packages/pycg-0.0.6-py3.10.egg/pycg/processing/base.py", line 61, in visit_Module
    self.generic_visit(node)
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 418, in generic_visit
    self.visit(item)
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/opt/homebrew/lib/python3.10/site-packages/pycg-0.0.6-py3.10.egg/pycg/processing/preprocessor.py", line 368, in visit_ClassDef
    super().visit_ClassDef(node)
  File "/opt/homebrew/lib/python3.10/site-packages/pycg-0.0.6-py3.10.egg/pycg/processing/base.py", line 126, in visit_ClassDef
    self.scope_manager.get_scope(self.current_ns).reset_counters()
AttributeError: 'NoneType' object has no attribute 'reset_counters'

@vitsalis
Copy link
Owner

I think there is a bigger underlying issue behind this and this throws the issue under the carpet. More specifically, why do we not have any scope element about such objects?

@gdrosos
Copy link
Collaborator Author

gdrosos commented Mar 27, 2023

Thanks!
I created on #51 a detailed description of the underlying issue.

Copy link
Owner

@vitsalis vitsalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool thanks!

@vitsalis vitsalis merged commit ab75e4f into vitsalis:master Mar 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants