From 98e501df2faa2a607c2545bfad72a017edd14870 Mon Sep 17 00:00:00 2001 From: thelicato Date: Tue, 17 Jan 2023 22:26:58 +0100 Subject: [PATCH 1/2] feat: improve python typing imports --- ts2py/__init__.py | 2 +- ts2py/main.py | 13 ++++++++----- ts2py/syntax/compiler.py | 30 ++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/ts2py/__init__.py b/ts2py/__init__.py index a3c86e3..8c56aa6 100644 --- a/ts2py/__init__.py +++ b/ts2py/__init__.py @@ -1,3 +1,3 @@ __title__ = "ts2py" __version__ = "0.0.1" -__description__ = "Python-Interoperability for Typescript-Interfaces" \ No newline at end of file +__description__ = "Python-Interoperability for Typescript-Interfaces" diff --git a/ts2py/main.py b/ts2py/main.py index 537df82..1a50953 100644 --- a/ts2py/main.py +++ b/ts2py/main.py @@ -20,7 +20,7 @@ """ import os -from typing import List, Tuple, Any +from typing import List, Tuple, Optional, Any import typer from DHParser import ( compile_source, @@ -76,6 +76,7 @@ def process_file(source: str, target: str) -> None: returned in the terminal output. """ if os.path.isfile(target): + Logger().info(f"Target file '{target}' already exists, deleting it...") os.remove(target) result, errors = compile_src(source) if not has_errors(errors, FATAL): @@ -83,6 +84,8 @@ def process_file(source: str, target: str) -> None: results_file.write(serialize_result(result)) if errors: Logger().error("\n".join(canonical_error_strings(errors))) + else: + Logger().success(f"Conversion for file '{source}' completed succesfully") @app.command() @@ -100,10 +103,10 @@ def convert( False, "--verbose", "-v", help="Enable verbose output" ), peps: List[types.args.PepArg] = typer.Option( - ["655"], "--peps", "-p", help="Assume Python-PEPs, e.g. 655" + ["655"], "--pep", "-p", help="Assume Python-PEPs, e.g. 655" ), - decorator: str = typer.Option( - "", "--decorator", "-d", help="Add the given decorator" + decorator: Optional[str] = typer.Option( + None, "--decorator", help="Add the given decorator" ), debug: bool = typer.Option(False, "--debug", "-d", help="Enable debug mode"), ): @@ -132,7 +135,7 @@ def convert( if helper.use_type_union(compatibility): set_preset_value("ts2py.UseTypeUnion", True, allow_new_key=True) # Set decorator - if len(decorator) > 0: + if decorator: set_preset_value("ts2py.ClassDecorator", decorator) # Set debug mode if debug: diff --git a/ts2py/syntax/compiler.py b/ts2py/syntax/compiler.py index 8011c20..e7d8124 100644 --- a/ts2py/syntax/compiler.py +++ b/ts2py/syntax/compiler.py @@ -23,9 +23,31 @@ def source_hash(source_text: str) -> str: return " ".join([md5(source_text), script_hash]) -DEFAULT_IMPORTS = """ -from typing import TypedDict, GenericTypedDict, NotRequired, Literal, Union, Optional, Any, Generic, TypeVar, Callable, Coroutine, List, Tuple, Dict -""" +TYPING_TYPES = [ + "NotRequired", + "Literal", + "Union", + "Optional", + "Any", + "Generic", + "TypeVar", + "Callable", + "Coroutine", + "List", + "Tuple", + "Dict", +] + + +def get_typing_imports(python_code: Any): + initial_import_line = "from typing import" + typing_types_to_add = ["TypedDict"] + for typing_type in TYPING_TYPES: + typing_match = re.search(rf"\b({typing_type})\b", python_code) + if typing_match: + typing_types_to_add.append(typing_type) + typing_types_str = ", ".join(typing_types_to_add) + return f"{initial_import_line} {typing_types_str}" def to_typename(varname: str) -> str: @@ -152,7 +174,7 @@ def prepare(self, root: Node) -> None: def finalize(self, python_code: Any) -> Any: code_blocks = [] if self.tree.name == "document": - code_blocks.append(DEFAULT_IMPORTS) + code_blocks.append(get_typing_imports(python_code)) code_blocks.append(python_code) cooked = "\n\n".join(code_blocks) cooked = re.sub(" +(?=\n)", "", cooked) From a78132d94314a2c3ad8aa2a34f510b5d790803ec Mon Sep 17 00:00:00 2001 From: thelicato Date: Tue, 17 Jan 2023 22:27:59 +0100 Subject: [PATCH 2/2] ver: bump version to 0.0.2 --- pyproject.toml | 2 +- ts2py/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 07337e4..0471bc1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ts2py" -version = "0.0.1" +version = "0.0.2" description = "Python-Interoperability for Typescript-Interfaces" license = "Apache-2.0" diff --git a/ts2py/__init__.py b/ts2py/__init__.py index 8c56aa6..ad13606 100644 --- a/ts2py/__init__.py +++ b/ts2py/__init__.py @@ -1,3 +1,3 @@ __title__ = "ts2py" -__version__ = "0.0.1" +__version__ = "0.0.2" __description__ = "Python-Interoperability for Typescript-Interfaces"