4

I'm using astropy's units and EartLocation to set my observatory location. Hardcoded for now.

However, when I run the code in VScode pylint is bugging me with errors whereas the code runs fine when I run it in PyCharm and IDLE.

Code:

obsy_location = EarthLocation(lat=52.91044*units.deg, lon=5.242664*units.deg, height=0*units.m)

Pylint is slapping me with 3 errors in that one line of code:

E1101:Module 'astropy.units' has no 'deg' member; maybe 'dex'?

E1101:Module 'astropy.units' has no 'm' member; maybe 'g'?

E1101:Module 'astropy.units' has no 'hour' member

Mind you, I am quite the beginner but since neither PyCharm nor IDLE seem to have any problems running this line I wonder what VScode's pylint is doing here. Can someone enlighten me?

I run VScode with the most recent Anaconda interpreter.

Community
  • 1
  • 1
theritz
  • 41
  • 5
  • 1
    It may be the pylint version. [This SO question](https://stackoverflow.com/questions/20553551/how-do-i-get-pylint-to-recognize-numpy-members/31465070) is likely the same in essence; one answer mentions this has been recently resolved (mid March 2018), at least for NumPy. See also [this Pylint GitHub issue](https://github.com/PyCQA/pylint/issues/779). –  Apr 16 '18 at 14:00
  • I would just tell pylint to ignore that line (though I wonder if aatropy could add some pylint directives to the units module to help it; I'm not sure the extent to which that's possible). A lot of the global namespace of Astropy's units module is not static, since it can support multiple default systems of units. So that makes things difficult for static source analysis tools. – Iguananaut Apr 16 '18 at 19:21

2 Answers2

3

Tools like pylint or PyCharm do static code analysis. They often do a good job for most Python code, but they fail in cases where the dynamic nature of Python is used.

In this case, astropy.units executes Python code on import to generate unit objects like deg (representing the unit "degree") or m (representing the unit "meter"). In this sense, it's expected and normal that you see "astropy.units has no member deg" warnings, because the static analysis tool doesn't execute the import, and thus those things don't exist in their analysis. I see the same warnings from PyCharm (which has it's own static analysis, doesn't execute PyLint in the background like in your case).

Now it is always possible to configure static analysis tools to ignore warnings of certain classes. Looking at https://stackoverflow.com/a/39500741/498873 , I'd expect this VS Code config setting to do the trick (but didn't try):

"python.linting.pylintArgs": [
    "--extension-pkg-whitelist=astropy.units"
]
Christoph
  • 2,790
  • 2
  • 18
  • 23
  • The option "--extension-pkg-whitelist" doesn't seem to disable the warning for me. Is it some kind of pylint extension that needs to be explicitly enabled? – Davor Cubranic Jun 08 '20 at 23:52
2

Late to the party, but this showed up when I was trying to solve the problem. Adding this to my settings works for me to turn off that pylint warning:

"python.linting.pylintArgs": [
    "--ignored-classes=astropy.units"
]
Julia Ebert
  • 1,583
  • 1
  • 21
  • 39
  • I was using a library (arcpy) which has a class (arcpy.da) that doesn't have all the usual python wrappers that provide for intellisense and whatnot. So pylint complained anytime arcpy.da was referenced - this solution worked great to selectively ignore anything relating to that class! – wmebane Aug 09 '18 at 20:08