strangetom / ingredient-parser

A tool to parse recipe ingredients into structured data
https://ingredient-parser.readthedocs.io/en/latest/
MIT License
73 stars 12 forks source link

TypeError: unsupported operand type(s) for |: 'type' and 'type' #3

Closed About2Golf closed 1 year ago

About2Golf commented 1 year ago

I just installed the package using pip but got a TypeError when importing the module.

`from ingredient_parser import parse_multiple_ingredients

parse_multiple_ingredients(["2 medium-large overripe bananas","2 large eggs"])`

The error message is:

from .parsers import parse_ingredient, parse_multiple_ingredients File "C:\Users-\AppData\Local\Programs\Python\Python39\lib\site-packages\ingredient_parser\parsers.py", line 10, in from .preprocess import PreProcessor File "C:\Users-\AppData\Local\Programs\Python\Python39\lib\site-packages\ingredient_parser\preprocess.py", line 44, in class PreProcessor: File "C:\Users-\AppData\Local\Programs\Python\Python39\lib\site-packages\ingredient_parser\preprocess.py", line 621, in PreProcessor def _token_features(self, index: int) -> dict[str, str | bool]: TypeError: unsupported operand type(s) for |: 'type' and 'type'

I was previously running this on Python 3.11 but I couldn't pip install due to python-crfsuite failing to build wheels. It worked when I deprecated my version to Python 3.9 but now I'm getting this module TypeError.

strangetom commented 1 year ago

This library only support Python 3.10 and later because the syntax for using the | operator for type hints was only introduced in Python 3.10.

If you really do need to use Python 3.9, then replacing all the instances of <type> | <type> with Union[type, type] should resolve the error you're getting.

strangetom commented 1 year ago

I've looked into this a bit more. If you're using Python 3.9 then pip will download beta2, which is the last version that indicates it supports Python 3.9. However it looks like I made a mistake and should have changed the supported Python versions for beta2 because it uses type hint syntax only available from Python 3.10. I've now yanked the version from pypi so other people don't have the same issue.

The library has changed significantly since beta2 so I recommend using Python 3.10 or later to get the latest version.

About2Golf commented 1 year ago

Thank you! Using Python 3.10 worked like a charm. And thanks for fixing the version for other future users. It's a great module. Good work.