E1101:Module 'turtle' has no 'forward' member
Asked Answered
E

2

6

I'm new to programming and I downloaded Python and got it running in Visual Studio Code. I was messing around with the turtle module and its functions.

The functions themselves work but pylint marks it as an error and says that there isn't a "member" like what I coded.

How would I go about fixing this error? (I don't want to set it to "ignore" the issue but rather recognize that the code I'm typing in is valid and comes from the turtle module)

Edwardoedwards answered 20/10, 2018 at 5:12 Comment(1)
Possible duplicate of How do I get PyLint to recognize numpy members?Godhood
C
15

The turtle module exposes two interfaces, a functional one and an object-oriented one. The functional interface is derived programatically from the object-oriented interface at load time, so static analysis tools can't see it, thus your pylint error. Instead of the functional interface:

import turtle

turtle.forward(100)

turtle.mainloop()

For which pylint generates no-member, try using the object-oriented interface:

from turtle import Screen, Turtle

screen = Screen()

turtle = Turtle()

turtle.forward(100)

screen.mainloop()

This particular import for turtle blocks out the functional interface and I recommend it as folks often run into bugs by mixing both the OOP and functional interaces.

Corium answered 20/10, 2018 at 7:33 Comment(1)
This was super helpful. Thanks! 👍Freeness
E
0

The functions mentioned are generated by code. PyLint only does static analysis.

I have written an astroid brain (plugin) to help the Python parser used by PyLint to add these functions.

Find the location where you have installed PyLint (directory ends with Lib\site-packages\pylint or Lib/site-packages/pylint.

Next to pylint is a directory astroid.

In the directory Lib\site-packages\astroid\brain or Lib/site-packages/astroid/brain create a file brain_turtle.py with the content:

import astroid

def register(linter):
  pass

def transform():
  import turtle
  def _make_global_funcs(functions, cls):
    funcs = []
    for methodname in functions:
      method = getattr(cls, methodname)
      paramslist, argslist = turtle.getmethparlist(method)
      if paramslist == "": continue
      funcs.append(f"def {methodname}{paramslist}: return")
    return funcs
  funcs = []
  funcs.extend(_make_global_funcs(turtle._tg_screen_functions, turtle._Screen))
  funcs.extend(_make_global_funcs(turtle._tg_turtle_functions, turtle.Turtle))
  return astroid.parse('\n'.join(funcs))

astroid.register_module_extender(astroid.MANAGER, "turtle", transform)

Depending on the IDE integration of PyLint you might need to restart the IDE.

I have also created an astroid issue to add this brain to the next update of PyLint (Astroid)

You can also use this file with the --load-plugins command line option of pylint. See the PyLint documentation for IDE integration. The used file needs to be on your PYTHONPATH

Expound answered 1/11, 2020 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.