[FR] Add Support for Python 3.13
Repository Feature
Core Repo - (rule management, validation, testing, lib, cicd, etc.)
Problem Description
Summary
Thanks @NC-Netrunner for walking through this issue with us!
Currently the repo does not support using Python 3.13. Users can install the necessary packages; however, when one tries to run Detection Rules CLI commands, an error similar to the following will occur:
detection-rules on main [?] is v0.4.22 via v3.13.2 (13_venv) on eric.forte
❯ python -m detection_rules --help
Traceback (most recent call last):
File "<frozen runpy>", line 189, in _run_module_as_main
File "<frozen runpy>", line 148, in _get_module_details
File "<frozen runpy>", line 112, in _get_module_details
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/detection_rules/__init__.py", line 13, in <module>
from . import ( # noqa: E402
...<15 lines>...
)
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/detection_rules/custom_rules.py", line 13, in <module>
from .docs import REPO_DOCS_DIR
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/detection_rules/docs.py", line 22, in <module>
from .packaging import Package
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/detection_rules/packaging.py", line 24, in <module>
from .navigator import NavigatorBuilder, Navigator
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/detection_rules/navigator.py", line 18, in <module>
from .mixins import MarshmallowDataclassMixin
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/detection_rules/mixins.py", line 21, in <module>
from .schemas import definitions
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/detection_rules/schemas/__init__.py", line 10, in <module>
import jsonschema
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/jsonschema/__init__.py", line 13, in <module>
from jsonschema._format import FormatChecker
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/jsonschema/_format.py", line 11, in <module>
from jsonschema.exceptions import FormatError
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/jsonschema/exceptions.py", line 15, in <module>
from referencing.exceptions import Unresolvable as _Unresolvable
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/referencing/__init__.py", line 5, in <module>
from referencing._core import Anchor, Registry, Resource, Specification
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/referencing/_core.py", line 18, in <module>
from referencing.typing import URI, Anchor as AnchorType, D, Mapping, Retrieve
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/referencing/typing.py", line 22, in <module>
D = TypeVar("D", default=Any)
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/typing_extensions.py", line 1455, in __new__
_set_default(typevar, default)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "/home/forteea1/Code/clean_mains/fix_dr/detection-rules/13_venv/lib/python3.13/site-packages/typing_extensions.py", line 1410, in _set_default
type_param.__default__ = typing._type_check(default, "Default must be a type")
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: attribute '__default__' of 'typing.TypeVar' objects is not writable
This error does not occur on Python 3.12
Desired Solution
We should add support for Python 3.13 when possible.
Considered Alternatives
No response
Additional Context
No response
To fix this you should just need to bump python-extenstions to 4.13.2. The error was fixed upstream in https://github.com/python/typing_extensions/issues/377:
we were creating instances of typing.TypeVar, then trying to monkeypatch default attributes onto the TypeVar instances after creating them. But it's unnecessary to monkeypatch attributes onto instances of TypeVar on Python 3.13+, because the "real" TypeVar implementation has a default parameter for its constructor on Python 3.13+. So I fixed the error in https://github.com/python/typing_extensions/pull/378 by simply not independently reimplementing TypeVar at all on Python 3.13+; instead, we now just from typing import TypeVar on Python 3.13+.
I updated and tested on 3.12 and 3.13 and both launched the CLI without error (that is as far as I went with testing).