Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

isinstance() with class defined in modules/ subdirectory #728

Unanswered
dbsdbs93 asked this question in General
Discussion options

Hello,

I hope that there is an answer or workaround to this issue. I'm not new to programming or the Python language, but I'm pretty thin on background on the Python runtime environment or how it changes within the Home Assistant ecosystem. So far I've reached my limits trying to figure something out, and I haven't found a similar discussion.

Inside of a class method defined in my [config]/pyscript/modules subdirectory, I check using isinstance whether an argument is of type, say, CustomClass, where CustomClass is also defined by me. The test fails unexpectedly. When I probe around in Jupyter, I can see that the class name has been "mangled":

In[2]: type(arg)
Out[2]: <class 'custom_components.pyscript.eval.CustomClass'>

By contrast, when I load the same code using command-line python (not Jupyter), the type is reported as modulename.CustomClass.

Unfortunately, as I think is covered briefly in the documentation, pyscript isn't actually defined, so I don't know how, or whether it's possible, to change the way I'm referring to the CustomClass type in my isinstance call--I definitely can't use custom_components.pyscript.eval.CustomClass since it's undefined. Or maybe there is a different approach that is worth a try.

I can probably go without isinstance although it complicates mypy type checking and probably will require an approach that comes with a few headaches. Any help would be appreciated.

EDIT: The original version of the post referenced "instanceof" instead of isinstance. I am using the correct function name in my code and in Jupyter, but somehow my brain typed instanceof when writing this post (which I think is... JavaScript? sigh).

You must be logged in to vote

Replies: 1 comment

Comment options

I think I've found a workaround, at least in the short term. I'd love to hear it if there might be a better way--the workaround, as you will see, is not the way Python's type-checking should be used. And it might require me to shut up mypy type-checking in some cases. But also it allows me to write logic that looks at the types of arguments, which is a better alternative to getting rid of isinstance-type logic altogether.

Instead of using isinstance(arg, CustomClass), I can use a string-based condition:

arg.__class__.__name__ == 'CustomClass'

I don't fully know what pitfalls this approach might lead to in general, but I think the chance of running into those with an application like this is probably low.

Fun fact: you can even get a list of strings representing the class hierarchy with:

[i.__name__ for i in list(arg.__class__.__mro__)]

I'm just starting with Pyscript, and it seems like almost literally the next best thing after sliced bread; I'm grateful for it. Thanks to anyone who reads this.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /