Open yousefmoazzam opened 1 month ago
Similar behaviour involving the |
operator in union types seems to be exhibited by the mock objects used when manually mocking imports (see #490):
WARNING: [autosummary] failed to import httomo.data.dataset_store.
Possible hints:
* AttributeError: module 'httomo.data' has no attribute 'dataset_store'
* ImportError:
* TypeError: unsupported operand type(s) for |: 'Mock' and 'CustomMock'
WARNING: [autosummary] failed to import httomo.data.padding.
Possible hints:
* TypeError: unsupported operand type(s) for |: 'Mock' and 'CustomMock'
* AttributeError: module 'httomo.data' has no attribute 'padding'
* ImportError:
WARNING: [autosummary] failed to import httomo.runner.dataset_store_backing.
Possible hints:
* ModuleNotFoundError: No module named 'numpy.typing'; 'numpy' is not a package
* ImportError:
* AttributeError: module 'httomo.runner' has no attribute 'dataset_store_backing'
WARNING: [autosummary] failed to import httomo.runner.task_runner.
Possible hints:
* TypeError: unsupported operand type(s) for |: 'Mock' and 'CustomMock'
* ImportError:
* AttributeError: module 'httomo.runner' has no attribute 'task_runner'
Yet another place where similar behaviour is exhibited is anytime |
is used with a type from h5py
.
Here's a dummy module (called bloop_two.py
) which uses h5py.Dataset
with:
|
Union
from typing import TypeAlias, Union
import h5py
MyType: TypeAlias = h5py.Dataset | int
MyOtherType: TypeAlias = Union[h5py.Dataset, int]
and in the |
case sphinx produces a warning:
WARNING: [autosummary] failed to import httomo.bloop_two.
Possible hints:
* TypeError: unsupported operand type(s) for |: 'Dataset' and 'type'
* AttributeError: module 'httomo' has no attribute 'bloop_two'
* ImportError:
but in the Union
case sphinx produces no warning.
As part of the warnings seen in #468, with a bit of trial and error I have narrowed down the cause of the "unsupported operand
|
" errors being produced by sphinx, despite running with a python 3.12 interpreter (which should recognise the|
operator as another way to doUnion
).It seems like mixing
|
and types that come from thexp
variable causes the issue. Ifxp
is not used, then the|
poses no problem for sphinx.I found this by being able reproduce the warning sphinx generates, but in a dummy module I created locally in my httomo repo and generating API docs for that module only.
The dummy module (which I lovingly called
bloop.py
) is the following:where you can see that I have defined
xp
as it is defined inutils.py
: https://github.com/DiamondLightSource/httomo/blob/f619ed123f2c5aad790ad75751d737c817a44196/httomo/utils.py#L11-L21Having
Blah
defined as:causes no issues, but having it defined as:
produces the warning:
Furthermore, note that
Thing
uses|
in its definition and that it does not produce the warning in any case.Therefore, I have concluded for now that "something" about using
xp
with|
isn't quite right, but what the "something" is I don't know.I don't fancy trying to dig into sphinx internals or python issues regarding
|
and variables whose values are modules, so I think a reasonable quick solution to this would be to useUnion
if a union type involvingxp
is needed, and to use|
in any other union type.