general: switch to using native 3.8 versions for cached_property/Literal/Protocol instead of compat

This commit is contained in:
Dima Gerasimov 2023-05-16 01:01:48 +01:00 committed by karlicoss
parent c34656e8fb
commit fe88380499
16 changed files with 29 additions and 98 deletions

View file

@ -366,10 +366,6 @@ def isoparse(s: str) -> tzdatetime:
return datetime.fromisoformat(s)
# legacy import -- we should use compat directly instead
from .compat import Literal
import re
# https://stackoverflow.com/a/295466/706389
def get_valid_filename(s: str) -> str:
@ -664,5 +660,7 @@ def assert_never(value: NoReturn) -> NoReturn:
assert False, f'Unhandled value: {value} ({type(value).__name__})'
# legacy deprecated import
from .compat import cached_property as cproperty
## legacy imports, keeping them here for backwards compatibility
from functools import cached_property as cproperty
from typing import Literal
##

View file

@ -3,6 +3,7 @@ Some backwards compatibility stuff/deprecation helpers
'''
import sys
from types import ModuleType
from typing import TYPE_CHECKING
from . import warnings
from .common import LazyLogger
@ -53,20 +54,10 @@ import os
windows = os.name == 'nt'
# keeping just for backwards compatibility, used to have compat implementation for 3.6
import sqlite3
def sqlite_backup(*, source: sqlite3.Connection, dest: sqlite3.Connection, **kwargs) -> None:
if sys.version_info[:2] >= (3, 7):
source.backup(dest, **kwargs)
else:
# https://stackoverflow.com/a/10856450/706389
import io
tempfile = io.StringIO()
for line in source.iterdump():
tempfile.write('%s\n' % line)
tempfile.seek(0)
dest.cursor().executescript(tempfile.read())
dest.commit()
source.backup(dest, **kwargs)
# can remove after python3.9
@ -76,55 +67,10 @@ def removeprefix(text: str, prefix: str) -> str:
return text
# can remove after python3.8
if sys.version_info[:2] >= (3, 8):
from functools import cached_property
else:
from typing import TypeVar, Callable
Cl = TypeVar('Cl')
R = TypeVar('R')
def cached_property(f: Callable[[Cl], R]) -> R:
import functools
return property(functools.lru_cache(maxsize=1)(f))
del Cl
del R
from typing import TYPE_CHECKING
if sys.version_info[:2] >= (3, 8):
from typing import Literal
else:
if TYPE_CHECKING:
from typing_extensions import Literal
else:
# erm.. I guess as long as it's not crashing, whatever...
class _Literal:
def __getitem__(self, args):
pass
Literal = _Literal()
if sys.version_info[:2] >= (3, 8):
from typing import Protocol
else:
if TYPE_CHECKING:
from typing_extensions import Protocol
else:
# todo could also use NamedTuple?
Protocol = object
if sys.version_info[:2] >= (3, 8):
from typing import TypedDict
else:
if TYPE_CHECKING:
from typing_extensions import TypedDict
else:
from typing import Dict
TypedDict = Dict
## used to have compat function before 3.8 for these
from functools import cached_property
from typing import Literal, Protocol, TypedDict
##
if sys.version_info[:2] >= (3, 10):

View file

@ -2,11 +2,10 @@ from __future__ import annotations
from .common import assert_subpackage; assert_subpackage(__name__)
from .common import PathIsh
from .compat import Protocol
from .sqlite import sqlite_connect_immutable
## sadly dataset doesn't have any type definitions
from typing import Iterable, Iterator, Dict, Optional, Any
from typing import Iterable, Iterator, Dict, Optional, Any, Protocol
from contextlib import AbstractContextManager

View file

@ -4,9 +4,7 @@ See https://beepb00p.xyz/mypy-error-handling.html#kiss for more detail
"""
from itertools import tee
from typing import Union, TypeVar, Iterable, List, Tuple, Type, Optional, Callable, Any, cast, Iterator
from .compat import Literal
from typing import Union, TypeVar, Iterable, List, Tuple, Type, Optional, Callable, Any, cast, Iterator, Literal
T = TypeVar('T')

View file

@ -5,7 +5,7 @@ Various pandas helpers and convenience functions
# NOTE: this file is meant to be importable without Pandas installed
from datetime import datetime
from pprint import pformat
from typing import Optional, TYPE_CHECKING, Any, Iterable, Type, Dict
from typing import Optional, TYPE_CHECKING, Any, Iterable, Type, Dict, Literal
from . import warnings, Res
from .common import LazyLogger, Json, asdict
@ -45,8 +45,6 @@ def check_dateish(s) -> Iterable[str]:
'''.strip()
from .compat import Literal
ErrorColPolicy = Literal[
'add_if_missing', # add error column if it's missing
'warn' , # warn, but do not modify

View file

@ -6,11 +6,10 @@ from pathlib import Path
import shutil
import sqlite3
from tempfile import TemporaryDirectory
from typing import Tuple, Any, Iterator, Callable, Optional, Union
from typing import Tuple, Any, Iterator, Callable, Optional, Union, Literal
from .common import PathIsh, assert_never
from .compat import Literal
def sqlite_connect_immutable(db: PathIsh) -> sqlite3.Connection:
@ -86,8 +85,7 @@ def sqlite_copy_and_open(db: PathIsh) -> sqlite3.Connection:
for p in tocopy:
shutil.copy(p, tdir / p.name)
with sqlite3.connect(str(tdir / dp.name)) as conn:
from .compat import sqlite_backup
sqlite_backup(source=conn, dest=dest)
conn.backup(target=dest)
conn.close()
return dest