PyCharm warning "End of statement expected" inside of type hints

Viewed 1726

I'm getting this weird "End of statement expected" warning inside of a comment, which functions as a Python 2/3 type hint:

enter image description here

Any ideas, what is wrong with those type-hints and why I am getting this warning?

I am using PyCharm Professional 2018.2.3 with Python 3.6 (Anaconda).

1 Answers

You can use forward references in your type hints with python >=3.5.

def resolve_notehead_wrt_staffline(notehead: 'CropObject', staffline_or_ledger_line: 'CropObject') -> int:
    """blahh blah"""
    from muscima.cropobject import CropObject
    ...

But even doing that won't fix the typehint in your case. This is where I would suggest not doing imports from within a function.

edit: I stewed on this a bit and realized it's easy to simply say "don't do that" but without knowing why isn't very helpful.

PEP8 says you're not supposed to import anywhere but at the top of the file, but every once in a while we have a "very good reason" for doing it elsewhere.

In your case, the function is expecting the module to have already been imported otherwise the argument objects wouldn't exist. With that being the case, you might as well put the import somewhere outside the scope of that function.

Related