Skip to content

Commit

Permalink
[*.py] Fix 3.6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMarks committed Dec 29, 2023
1 parent c6bfc58 commit 4ed0a27
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 123 deletions.
9 changes: 5 additions & 4 deletions cdd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
"""

import logging
from logging import Logger
from logging import getLogger as get_logger

__author__ = "Samuel Marks"
__version__ = "0.0.99rc15"
__author__ = "Samuel Marks" # type: str
__version__ = "0.0.99rc15" # type: str
__description__ = (
"Open API to/fro routes, models, and tests. "
"Convert between docstrings, classes, methods, argparse, pydantic, and SQLalchemy."
)
) # type: str


root_logger = get_logger()
root_logger: Logger = get_logger()
logging.getLogger("blib2to3").setLevel(logging.WARNING)

__all__ = [
Expand Down
30 changes: 15 additions & 15 deletions cdd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
`__main__` implementation, can be run directly or with `python -m cdd`
"""

from argparse import ArgumentParser, Namespace
from argparse import ArgumentParser, Namespace, _SubParsersAction
from codecs import decode
from collections import deque
from itertools import chain, filterfalse
Expand All @@ -31,7 +31,7 @@
"sqlalchemy",
"sqlalchemy_hybrid",
"sqlalchemy_table",
)
) # type: tuple[str, ...]


def _build_parser():
Expand All @@ -51,7 +51,7 @@ def _build_parser():
version="%(prog)s {__version__}".format(__version__=__version__),
)

subparsers = parser.add_subparsers()
subparsers: _SubParsersAction[ArgumentParser] = parser.add_subparsers()
subparsers.required = True
subparsers.dest = "command"

Expand Down Expand Up @@ -120,7 +120,7 @@ def _build_parser():
########
# Sync #
########
sync_parser = subparsers.add_parser(
sync_parser: ArgumentParser = subparsers.add_parser(
"sync", help="Force argparse, classes, and/or methods to be equivalent"
)

Expand Down Expand Up @@ -195,7 +195,7 @@ def _build_parser():
#######
# Gen #
#######
gen_parser = subparsers.add_parser(
gen_parser: ArgumentParser = subparsers.add_parser(
"gen",
help=(
"Generate classes, functions, argparse function, sqlalchemy tables and/or sqlalchemy classes"
Expand Down Expand Up @@ -275,7 +275,7 @@ def _build_parser():
##############
# gen_routes #
##############
routes_parser = subparsers.add_parser(
routes_parser: ArgumentParser = subparsers.add_parser(
"gen_routes", help="Generate per model route(s)"
)

Expand Down Expand Up @@ -311,7 +311,7 @@ def _build_parser():
###########
# openapi #
###########
openapi_parser = subparsers.add_parser(
openapi_parser: ArgumentParser = subparsers.add_parser(
"openapi", help="Generate OpenAPI schema from specified project(s)"
)

Expand All @@ -335,7 +335,7 @@ def _build_parser():
############
# doctrans #
############
doctrans_parser = subparsers.add_parser(
doctrans_parser: ArgumentParser = subparsers.add_parser(
"doctrans",
help=(
"Convert docstring format of all classes and functions within target file"
Expand Down Expand Up @@ -378,7 +378,7 @@ def _build_parser():
#########
# exmod #
#########
exmod_parser = subparsers.add_parser(
exmod_parser: ArgumentParser = subparsers.add_parser(
"exmod",
help=(
"Expose module hierarchy->{functions,classes,vars} for parameterisation "
Expand Down Expand Up @@ -450,12 +450,12 @@ def main(cli_argv=None, return_args=False):
:return: the args if `return_args`, else None
:rtype: ```Optional[Namespace]```
"""
_parser = _build_parser()
args = _parser.parse_args(args=cli_argv)
command = args.command
_parser: ArgumentParser = _build_parser()
args: Namespace = _parser.parse_args(args=cli_argv)
command: str = args.command
args_dict = {k: v for k, v in vars(args).items() if k != "command"}
if command == "sync":
args = Namespace(
args: Namespace = Namespace(
**{
k: v
if k in frozenset(("truth", "no_word_wrap"))
Expand All @@ -466,13 +466,13 @@ def main(cli_argv=None, return_args=False):
}
)

truth_file = getattr(args, pluralise(args.truth))
truth_file: str = getattr(args, pluralise(args.truth))
require_file_existent(
_parser, truth_file[0] if truth_file else truth_file, name="truth"
)
truth_file: str = path.realpath(path.expanduser(truth_file[0]))

number_of_files = sum(
number_of_files: int = sum(
len(val)
for key, val in vars(args).items()
if isinstance(val, list) and not key.endswith("_names")
Expand Down
3 changes: 1 addition & 2 deletions cdd/argparse_function/parse.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Argparse function parser
"""
from _ast import Call
from ast import Assign, FunctionDef, Return, Tuple, get_docstring
from ast import Assign, Call, FunctionDef, Return, Tuple, get_docstring
from collections import OrderedDict
from functools import partial
from itertools import filterfalse
Expand Down
5 changes: 3 additions & 2 deletions cdd/class_/utils/shared_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"""

from ast import ClassDef, Module
from typing import List, Optional, Tuple, Union

from cdd.shared.pure_utils import PY_GTE_3_8
from cdd.shared.types import IntermediateRepr

if PY_GTE_3_8:
from typing import List, Literal, Optional, Protocol, Tuple, Union
from typing import Literal, Protocol
else:
from typing_extensions import Protocol
from typing_extensions import Literal, Protocol


class ClassEmitProtocol(Protocol):
Expand Down
3 changes: 1 addition & 2 deletions cdd/compound/doctrans.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Helper to traverse the AST of the input file, extract the docstring out, parse and format to intended style, and emit
"""
from _ast import Module
from ast import fix_missing_locations
from ast import Module, fix_missing_locations
from copy import deepcopy
from operator import attrgetter
from typing import List, NamedTuple
Expand Down
3 changes: 1 addition & 2 deletions cdd/compound/exmod_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import ast
import sys
from _ast import AST
from ast import Assign, Expr, ImportFrom, List, Load, Module, Name, Store, alias
from ast import AST, Assign, Expr, ImportFrom, List, Load, Module, Name, Store, alias
from collections import OrderedDict, defaultdict, deque
from functools import partial
from inspect import getfile, ismodule
Expand Down
5 changes: 2 additions & 3 deletions cdd/compound/openapi/gen_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import ast
from ast import Attribute, Call, ClassDef, FunctionDef, Module, Name
from collections.abc import dict_keys
from itertools import chain
from operator import attrgetter, itemgetter
from os import path
Expand Down Expand Up @@ -206,9 +205,9 @@ def get_names(functions):
filter(rpartial(isinstance, FunctionDef), ast.walk(mod)),
)
)
missing_routes: dict_keys[str, str] = (
missing_routes = (
routes_required.keys() & routes_existing.keys() ^ routes_required.keys()
)
) # type: dict_keys[str, str]

if not missing_routes:
return
Expand Down
9 changes: 8 additions & 1 deletion cdd/json_schema/utils/shared_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
Shared utility functions for JSON schema
"""

from typing import Any, Dict, List, Literal, TypedDict
from typing import Any, Dict, List

from cdd.shared.pure_utils import PY_GTE_3_8

if PY_GTE_3_8:
from typing import Literal, TypedDict
else:
from typing_extensions import Literal, TypedDict

JSON_property = TypedDict(
"JSON_property",
Expand Down
3 changes: 3 additions & 0 deletions cdd/shared/docstring_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from typing import * # noqa
from typing import Dict, List, Tuple, Union

if sys.version_info[:2] < (3, 8):
from typing_extensions import TypedDict

from cdd.docstring.utils.emit_utils import interpolate_defaults
from cdd.docstring.utils.parse_utils import parse_adhoc_doc_for_typ
from cdd.shared.ast_utils import NoneStr, get_value
Expand Down
Loading

0 comments on commit 4ed0a27

Please sign in to comment.