Skip to content

Fix the fastai example when using Python 3.9 #307

@c-bata

Description

@c-bata

Expected behavior

After the release of fastai 2.8.0, the CI running python fastai/fastai_simple.py started failing when using Python 3.9. To temporarily resolve the issue, I opened a PR to skip Python 3.9. This issue proposes reintroducing Python 3.9 and resolving the failure.
https://github.com/optuna/optuna-examples/actions/runs/13926561255/job/38972540655

Environment

  • Optuna version: 4.2.0
  • Python version: 3.9.0
  • OS: ubuntu
  • (Optional) Other libraries and their versions: fastai>=2.8.0

Error messages, stack traces, or logs

python fastai/fastai_simple.py
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/beartype/_check/forward/fwdmain.py", line 739, in _resolve_func_scope_forward_hint
    hint_resolved = eval(hint, decor_meta.func_wrappee_scope_forward)
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'BypassNewMeta' and 'torch._C._TensorMeta'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/work/optuna-examples/optuna-examples/fastai/fastai_simple.py", line 22, in <module>
    from fastai.vision.all import accuracy
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/fastai/vision/all.py", line 4, in <module>
    from .augment import *
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/fastai/vision/augment.py", line 8, in <module>
    from .core import *
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/fastai/vision/core.py", line 259, in <module>
    class PointScaler(Transform):
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/fasttransform/transform.py", line 75, in __new__
    if funcs: setattr(new_cls, nm, _merge_funcs(*funcs))
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/fasttransform/transform.py", line 42, in _merge_funcs
    res = Function(fs[-1].methods[0].implementation)
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/plum/function.py", line [18](https://github.com/optuna/optuna-examples/actions/runs/13926561255/job/38972540655#step:5:19)1, in methods
    self._resolve_pending_registrations()
  File "/opt/hostedtoolcache/Python/3.9.[21](https://github.com/optuna/optuna-examples/actions/runs/13926561255/job/38972540655#step:5:22)/x64/lib/python3.9/site-packages/plum/function.py", line 280, in _resolve_pending_registrations
    signature = Signature.from_callable(f, precedence=precedence)
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/plum/signature.py", line 88, in from_callable
    types, varargs = _extract_signature(f)
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/plum/signature.py", line 346, in _extract_signature
    resolve_pep563(f)
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/plum/signature.py", line 329, in resolve_pep563
    beartype_resolve_pep563(f)  # This mutates `f`.
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/beartype/peps/_pep563.py", line [26](https://github.com/optuna/optuna-examples/actions/runs/13926561255/job/38972540655#step:5:27)3, in resolve_pep563
    arg_name_to_hint[arg_name] = resolve_hint(
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/beartype/_check/forward/fwdmain.py", line 308, in resolve_hint
    return _resolve_func_scope_forward_hint(
  File "/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/beartype/_check/forward/fwdmain.py", line 855, in _resolve_func_scope_forward_hint
    raise exception_cls(exception_message) from exception
beartype.roar.BeartypeDecorHintPep604Exception: Stringified PEP 604 type hint 'PILBase | TensorImageBase' syntactically invalid under Python < 3.10 (i.e., TypeError("unsupported operand type(s) for |: 'BypassNewMeta' and 'torch._C._TensorMeta'")). Consider either:
* Requiring Python >= 3.10. Abandon Python < 3.10 all ye who code here.
* Refactoring PEP 604 type hints into equivalent PEP 484 type hints: e.g.,
    # Instead of this...
    from __future__ import annotations
    def bad_func() -> int | str: ...

    # Do this. Ugly, yet it works. Worky >>>> pretty.
    from typing import Union
    def bad_func() -> Union[int, str]: ...
Error: Process completed with exit code 1.

Steps to reproduce

  1. Install fastai>=2.8.0 on Python 3.9
  2. Run python fastai/fastai_simple.py

Reproducible examples (optional)

Please checkout the CI workflow file.

Additional context (optional)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue/PR about behavior that is broken. Not for typos/CI but for example itself.staleExempt from stale bot labeling.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions