Using asserts for type hinting

Answered

In PyCharm 2016.2, If there was some code like `df = pandas.concat(df_list)`, you could use `assert isinstance(df, pandas.DataFrame)` to do both runtime type checking and type hinting for PyCharm, but in 2016.3 you have to do `# type: pandas.DataFrame` for type hinting for that line of code.

So in order to do both runtime checking and type hinting you would have to do:

df = pandas.concat(df_list)  # type: pandas.DataFrame
assert isinstance(df, pandas.DataFrame)

vs just

df = pandas.concat(df_list)
assert isinstance(df, pandas.DataFrame)

 

which was possible in the previous version.

 

Is there a reason that this was removed? Or is there a way that this can be turned on as a default option?, since this would force me to add a lot of unnecessary code, or remove type hinting. This is especially problematic for pandas related code, since a lot of the functions work on multiple types so the return types are often ambiguous.

 

type hinting also doesn't work anymore for cases like:

if not isinstance(obj, str):
    raise TypeError

as well.

 

Edit:

Apparently this problem only occurs when a value has a type hint from a function, but later is trying to be overridden by an assert.

For example in:

df = pandas.concat(df_list)
assert isinstance(df, pandas.DataFrame)

pandas.concat has a return type hint of `type`, so df has the inferred type `type` from the function, but after the assert, instead of changing to inferred type `DataFrame` df stays as inferred type `type` which was originally assigned from the function.

In cases like:

version = payload['version']
assert isinstance(version, int)

then version has the correct inferred type after the assert, since payload which is a dict, returns an inferred type `Any` due to not having any annotations.

 

Could there be some clarification about how the new type hinting system works? or whether there are plans to leave it as is or to change it in the future, since it has changed since the previous version?

3
5 comments

I hope that's not true.

I'm still on 2016.2 for the moment, but a whole bunch of our code has asserts that act as both debug and a means to type hint in editor.

Is there a new option setting that enables the type hinting perhaps?

0

Thanks for the update. I can see how that might be a bit confusing. 

I don't use pandas myself, so I didn't have a chance to see that specific example; but I'm curious about why the concat method has a different typehint from your assert.  I would think one of them would be incorrect.  Assuming your script runs, I would guess the assert is correct but the typehint in pandas is wrong?  Am I mistaken?

0

Hi there, sorry for the late response.

 

At first glance, it seems that this happens only if the type of an expression inferred before a type assertion is itself "type", no matter where it came from. Others are successfully overridden by the following dynamic type check via "isinstance()". We'll figure out what's going on and try to fix the issue in the upcoming release.

Thanks for reporting the problem!

0

Any update on this?

0

Could you please give us an example where you still encounter the problem? I'm no longer able to reproduce the original one in PyCharm 2018.3.

0

Please sign in to leave a comment.