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

PyCharm IDE cannot find references to unit classes #142

Open
jamesbraza opened this issue Mar 3, 2020 · 7 comments
Open

PyCharm IDE cannot find references to unit classes #142

jamesbraza opened this issue Mar 3, 2020 · 7 comments

Comments

@jamesbraza
Copy link

jamesbraza commented Mar 3, 2020

  • unyt version: 2.7.1
  • Python version: 3.6.9
  • Operating System: macOS Mojave v10.14.3
  • PyCharm version: 2019.2.6 CE

Description

Since unit instances are added to unyt's namespace at runtime, PyCharm (Python IDE) cannot introspect the attributes of a given unit. PyCharm declares "Cannot find reference".

I would like to be able to use PyCharm's introspection to aid in developer speed (code completion) and decrease the chances of attribute errors.

Possible solution: this question suggests making a stub file. I am not sure if this is feasible.

What I Did

See this question on Stack Overflow.

from unyt import degC

t1 = 0 * degC
t1.convert_to_base()

image

@ngoldbaum
Copy link
Member

I wonder if we dynamically created an __all__ attribute at import time, would PyCharm be able to resolve the references then? I don't have PyCharm installed so can't really help much with debugging this one.

If PyCharm is doing this purely using text analysis then I don't think there's much we can do. We're definitely not going to go to explicitly listing everything since that would be a maintainance nightmare.

@JBorrow
Copy link
Contributor

JBorrow commented Mar 3, 2020

This is also a problem in Visual Studio Code, IIRC.

@jamesbraza
Copy link
Author

jamesbraza commented Mar 3, 2020

@JBorrow what do you do? Just ignore it? Do you ever use code completion with unit instances?

@ngoldbaum I installed unyt and inserted __all__ = ["degC"] into unyt.__init__.py.

The result is my base Python file now no longer complains "Cannot find reference", it now knows that degC comes from unyt. However, it still doesn't know that degC is a Unit instance. PyCharm just suggests that it's an object. It does not suggest the method .copy or the property .units while typing.

@ngoldbaum
Copy link
Member

Another idea that might work is to generate __init__.py at build time (e.g. orchestrated by setup.py) and replace the globals manipulation with code literals.

@JBorrow
Copy link
Contributor

JBorrow commented Mar 4, 2020

@JDusub83

I in general just ignore this, yes. Usually I am working inside a function with type hints:

image

The red underline is telling me that Mpc is not found in unyt.

@jamesbraza
Copy link
Author

Back to the __all__ approach. Adding a global variable type annotation on the units right after __all__ solves the problem... PyCharm can now infer the type!

Inside unyt.__init__.py:

__all__ = ["degC"]
degC: Unit

I am unsure which is better. Does anyone have any thoughts?

  • Adding each unit to an __all__ attribute and specifying a global variable type annotation
  • Replacing globals manipulation with code literals

@ngoldbaum
Copy link
Member

Is it possible to dynamically define type annotations? If so, does PyCharm recognize those? It's also not clear to me from your examples that PyCharm would know how to deal with a dynamically defined __all__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants