valknight / Cohost.py

A python library for Cohost.org!
MIT License
58 stars 8 forks source link

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

Open JPLeBreton opened 11 months ago

JPLeBreton commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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.

valknight commented 11 months ago

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 :)

JPLeBreton commented 10 months ago

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