Skip to content
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

False-positive name-error when using @dataclass decorator #1546

Open
cebtenzzre opened this issue Dec 13, 2023 · 1 comment
Open

False-positive name-error when using @dataclass decorator #1546

cebtenzzre opened this issue Dec 13, 2023 · 1 comment
Labels
bug cat: control flow control flow analysis

Comments

@cebtenzzre
Copy link

Software versions

Python 3.11.3
pytype 2023.12.08

Testcase

from dataclasses import dataclass

class VocabLoader:
    def __init__(self):
        find_vocab_file_path()

@dataclass
class ModelPlus:
    vocab: VocabLoader

def find_vocab_file_path():
    pass

Description

$ pytype-single repr.py
File "repr.py", line 5, in __init__: Name 'find_vocab_file_path' is not defined [name-error]
Called from (traceback):
  line 7, in current file

For more details, see https://google.github.io/pytype/errors.html#name-error
@rchen152 rchen152 added bug cat: control flow control flow analysis labels Dec 14, 2023
@rchen152
Copy link
Contributor

This is happening because pytype attempts to instantiate VocabLoader while analyzing ModelPlus, before seeing find_vocab_file_path. You can work around it by moving find_vocab_file_path before ModelPlus in the source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cat: control flow control flow analysis
Projects
None yet
Development

No branches or pull requests

2 participants