1
class Folder:

    def copy_files_with(self, extension: str, to_location: Folder):
        pass

    def __eq__(self, other):
        if isinstance(other, Folder):
            return (self._parent, self._subdirectory) == (other._parent, other._subdirectory)
        return NotImplemented

    def __hash__(self):
        return hash((self._parent, self._subdirectory))

I'm using Visual Studio code with PyLint and it returns an error with the copy_files_with method.

line 20, in Folder
def copy_files_with(self, extension: str, to_location: Folder)

I removed all the unnecessary code, but line 20 is where the method copy_files_with is located.

I don't understand why, the __eq__ method can see the Folder class in the isinstance call. I want the to_location to be a Folder, and I want to specify that in the type hint, how do I do this?

киска
  • 137
  • 1
  • 7
  • Are you using the built-in [pylint](https://code.visualstudio.com/docs/python/linting#_pylint) linting tool with VS Code, or are you manually running pylint (from command line, for example)? I can't seem to reproduce your problem on my VS Code. Do you have a pylintrc? Did you configure any `python.linting.*` settings? – Gino Mempin Jul 14 '19 at 03:31
  • @GinoMempin - Built-in – киска Jul 14 '19 at 03:32
  • @GinoMempin - Nope, I installed pylint because VSCode recommended it, other than that I didn't touch any other configuration settings. – киска Jul 14 '19 at 03:34
  • Even with all pylint checks enabled, I can't reproduce your problem. Where did you see the `NameError` error message? That doesn't look like a pylint error, it looks more like a runtime error. – Gino Mempin Jul 14 '19 at 03:35
  • @GinoMempin - See update, its says Line 20 and prints the method name along with `NameError`. Even when I run it from `cmd.exe` **without** pylint, same thing. – киска Jul 14 '19 at 03:40
  • @GinoMempin - Removing the `Folder` type hint seems to fix the problem. I really would like to use a type hint rather than a comment or docstring. – киска Jul 14 '19 at 03:43
  • @GinoMempin - The latest version, but the answer below and duplicate link explained it. Thanks anyways. – киска Jul 14 '19 at 03:51

1 Answers1

2

When a class and its methods are processed, in the moment a module is imported, the lines containing the "class" and "def" statements are run. The method bodies - the code inside the "def" blocks, are only run when the class is later instantiated and have its methods called.

So, it is natural that a class is not defined inside its own body - the class body have to be processed before Python will create the class itself - and only then it will be associated with its name.

The way to make this work - references to the own class as well as refrences to class that are forward in the same file, is to use their name as strings in the annotations - in this case, you should use

 def copy_files_with(self, extension: str, to_location: 'Folder'):

and it should work with most tools that make use of the annotations.

jsbueno
  • 99,910
  • 10
  • 151
  • 209