patrick-kidger / torchtyping

Type annotations and dynamic checking for a tensor's shape, dtype, names, etc.
Apache License 2.0
1.41k stars 34 forks source link

pycharm warning of Unresolved reference #23

Closed chris-tkinter closed 3 years ago

chris-tkinter commented 3 years ago

Hi, I really like this project! It's mind blowing for me!!

I have a question. When coding in pycharm using TensorType with a string dimension name, I got a warning about unresolved reference

image

I am not sure if this is a limitation of pycharm. I am using pycharm 2021.1.1, python 3.7.10 and torchtyping 0.1.4.

Thanks!

patrick-kidger commented 3 years ago

This is because strings in types are often used to denote forward references, e.g.

def f(x: "A"): ...

class A: ...

As a result PyCharm is looking for an object called batch somewhere, and is complaining that it can't find it.

This is essentially a limitation of PyCharm -- it's assuming strings are being used in a way that they're only sometimes used.

One option is to disable the warning, via whatever mechanism PyCharm offers for that. It's been a while since I've used PyCharm but I think you should be able to disable the warning locally, or for the whole file, or globally.

The other option is just to put batch = None anywhere in the file to fool PyCharm into thinking that something called batch exists.

chris-tkinter commented 3 years ago

Thanks! That makes sense!

fzyzcjy commented 1 year ago

Hi, is there any workarounds that does not require disabling linting or creating weird variables? thanks!