Skip to content

Python 3.9 - TypeError: unsupported operand type(s) for |: 'TypeVar' and 'NoneType' #2023

Description

It seems onnxscript should be supported for Python 3.8+ but the | operator for type annotation was only added in Python 3.10 but still widely used in the repository. So onnxscript may not really work for Python < 3.10. Is that correct?

I started seeing this error in onnxscript==0.1.0.dev20250120 with Python 3.9 (3.10+ works fine) which was not the case in onnxscript==0.1.0.dev20250118 when doing torch.onnx.dynamo_export with torch==2.5.1.

    torch.onnx.dynamo_export(
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/torch/onnx/__init__.py:517: in dynamo_export
    return dynamo_export(
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/torch/onnx/_internal/_exporter_legacy.py:1206: in dynamo_export
    else ResolvedExportOptions(export_options, model=model)
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/torch/onnx/_internal/_exporter_legacy.py:360: in __init__
    self.onnx_registry = resolve(options.onnx_registry, OnnxRegistry())
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/torch/onnx/_internal/_exporter_legacy.py:121: in __init__
    self._initiate_registry_from_torchlib()
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/torch/onnx/_internal/_exporter_legacy.py:139: in _initiate_registry_from_torchlib
    for meta in onnxscript_apis.get_torchlib_ops():
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/onnxscript/_framework_apis/torch_2_5.py:119: in get_torchlib_ops
    domain=overload_func.function_ir.domain,
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/onnxscript/values.py:657: in function_ir
    return converter.translate_function_signature(func_ast)
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/onnxscript/converter.py:1470: in translate_function_signature
    return self._translate_function_signature_common(fn)
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/onnxscript/converter.py:1397: in _translate_function_signature_common
    typeinfo = self._eval_constant_expr(x.annotation)
/tmp/-modelopt-tox/py39-torch25-unit/lib/python3.9/site-packages/onnxscript/converter.py:467: in _eval_constant_expr
    return eval(cpl, self.globals, locals)  # pylint: disable=eval-used
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
>   ???
E   TypeError: unsupported operand type(s) for |: 'TypeVar' and 'NoneType'
<ast>:2: TypeError

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions