Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around various issues with Python 3.12 #222

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python-spec/src/somacore/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import enum
from typing import Any, Dict, Mapping, Optional, Sequence, TypeVar, Union
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple, TypeVar, Union

import attrs
import numpy as np
Expand Down Expand Up @@ -175,6 +175,7 @@ class ResultOrder(enum.Enum):

SparseNDCoord = Union[
None,
Tuple[int, ...],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is necessary that’s fine, I guess, but this should already be covered under ValSliceOrSequence[int], since that includes a Sequence[int], which is a supertype of Tuple[int, ...].

ValSliceOrSequence[int],
npt.NDArray[np.integer],
pa.IntegerArray,
Expand Down
11 changes: 11 additions & 0 deletions python-spec/src/somacore/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from concurrent import futures
from typing import (
TYPE_CHECKING,
Any,
NoReturn,
Optional,
Sequence,
Expand Down Expand Up @@ -85,6 +86,16 @@ def stop(self) -> Optional[_T_co]: ...
@property
def step(self) -> Optional[_T_co]: ...

if sys.version_info >= (3, 12):

@property
def __orig_bases__(self) -> Any:
# Only exists in Python 3.12 and above
from types import get_original_bases

return get_original_bases(int)
# return get_original_bases(_T_co)
Comment on lines +91 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typeguard bug—it’s checking too many built-in attributes of the protocol that don’t actually matter. (See, for example, a case where it rejects objects with a completely empty protocol that I found.

That said, if implementing this makes typeguard stop complaining for now, I would do two things:

  1. Add a comment to that effect, and that it should be removed at some point
  2. Make this function instead raise AssertionError, since it should never be called.


if sys.version_info < (3, 10) and not TYPE_CHECKING:
# Python 3.9 and below have a bug where any Protocol with a @property
# was always regarded as runtime-checkable.
Expand Down
Loading