Cohost.py icon indicating copy to clipboard operation
Cohost.py copied to clipboard

[Python 3.9 compatibility issue] TypeError: unsupported operand type(s) for |: 'types.GenericAlias' and 'type'

Open JPLeBreton opened this issue 2 years ago • 4 comments

Running from cohost.models.user import User, I get this error:

  File "/home/jpl/.local/lib/python3.9/site-packages/cohost/models/user.py", line 6, in <module>
    from cohost.models.project import EditableProject
  File "/home/jpl/.local/lib/python3.9/site-packages/cohost/models/project.py", line 3, in <module>
    from cohost.models.block import AttachmentBlock
  File "/home/jpl/.local/lib/python3.9/site-packages/cohost/models/block.py", line 5, in <module>
    from cohost.network import fetch, generate_login_cookies
  File "/home/jpl/.local/lib/python3.9/site-packages/cohost/network.py", line 75, in <module>
    def fetchTrpc(methods: list[str] | str, cookie: str,
TypeError: unsupported operand type(s) for |: 'types.GenericAlias' and 'type'

I don't recognize this error. Python 3.9 is supported by this module, right?

JPLeBreton avatar Dec 06 '23 20:12 JPLeBreton

Ah, looks like it's specifically the union operator added to type annotation in Python 3.10: https://www.blog.pythonlibrary.org/2021/09/11/python-3-10-simplifies-unions-in-type-annotations

The machine I'm trying to run this on (an old first gen Raspberry Pi) is unlikely to get an upgrade from Python 3.9 to 3.10, but I also don't want to ask you to make your code worse.

JPLeBreton avatar Dec 06 '23 23:12 JPLeBreton

It does look like that's the only usage of the | operator in the code; turning the one line with it into its un-annotated type equivalent allowed from cohost.models.user import User to succeed on this machine. So I have a workaround for now. Do with this issue as you like.

JPLeBreton avatar Dec 07 '23 06:12 JPLeBreton

Given the first gen Pi is a fairly understandable piece of hardware for this project, I'd consider this a bug, but, you are right in this probably making the code a little worse...

Asking as you have a pretty good use case - would you consider it OK to have a seperate branch with Python 3.9 compatibility, and then installing that branch via Git? If so, I might do that as a compromise :)

valknight avatar Dec 14 '23 13:12 valknight

A python 3.9 branch for those that need it seems like a fine solution to me, thank you!

JPLeBreton avatar Dec 14 '23 20:12 JPLeBreton