core: migrate code to benefit from 3.9 stuff (#401)

for now keeping ruff on 3.8 target version, need to sort out modules as well
This commit is contained in:
karlicoss 2024-10-19 20:55:09 +01:00 committed by GitHub
parent bc7c3ac253
commit d3f9a8e8b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
43 changed files with 515 additions and 404 deletions

View file

@ -10,6 +10,8 @@ This potentially allows it to be:
It should be free of external modules, importlib, exec, etc. etc.
'''
from __future__ import annotations
REQUIRES = 'REQUIRES'
NOT_HPI_MODULE_VAR = '__NOT_HPI_MODULE__'
@ -19,8 +21,9 @@ import ast
import logging
import os
import re
from collections.abc import Iterable, Sequence
from pathlib import Path
from typing import Any, Iterable, List, NamedTuple, Optional, Sequence, cast
from typing import Any, NamedTuple, Optional, cast
'''
None means that requirements weren't defined (different from empty requirements)
@ -30,11 +33,11 @@ Requires = Optional[Sequence[str]]
class HPIModule(NamedTuple):
name: str
skip_reason: Optional[str]
doc: Optional[str] = None
file: Optional[Path] = None
skip_reason: str | None
doc: str | None = None
file: Path | None = None
requires: Requires = None
legacy: Optional[str] = None # contains reason/deprecation warning
legacy: str | None = None # contains reason/deprecation warning
def ignored(m: str) -> bool:
@ -55,13 +58,13 @@ def has_stats(src: Path) -> bool:
def _has_stats(code: str) -> bool:
a: ast.Module = ast.parse(code)
for x in a.body:
try: # maybe assign
try: # maybe assign
[tg] = cast(Any, x).targets
if tg.id == 'stats':
return True
except:
pass
try: # maybe def?
try: # maybe def?
name = cast(Any, x).name
if name == 'stats':
return True
@ -144,7 +147,7 @@ def all_modules() -> Iterable[HPIModule]:
def _iter_my_roots() -> Iterable[Path]:
import my # doesn't import any code, because of namespace package
paths: List[str] = list(my.__path__)
paths: list[str] = list(my.__path__)
if len(paths) == 0:
# should probably never happen?, if this code is running, it was imported
# because something was added to __path__ to match this name