From 65ef228def19a39119fdcf68170fac603b99cafc Mon Sep 17 00:00:00 2001 From: Lev Gorodetskiy Date: Mon, 27 Sep 2021 17:40:48 +0300 Subject: [PATCH] Fix check of existing types, rename arguments with the same name on codegen (#141) * Fix typos in codegen module * Avoid duplicate arguments * Changelog * Changelog --- CHANGELOG.md | 6 ++++-- src/dipdup/codegen.py | 2 +- src/dipdup/config.py | 23 +++++++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 103da71cf..34b6f1128 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 3.0.2 - 2021-09-25 +## [unreleased] ### Added @@ -15,7 +15,9 @@ * Removed unnecessary calls to TzKT API during the partial sync. * Fixed removal of PostgreSQL extensions (`timescaledb`, `pgcrypto`) by function `truncate_database` triggered on reindex. * Fixed updating relation between index and head in DB. -* Fixed creation of missing project package +* Fixed creation of missing project package on `init`. +* Fixed invalid handler callbacks generated on `init`. +* Fixed detection of existing types in the project. ## 3.0.1 - 2021-09-24 diff --git a/src/dipdup/codegen.py b/src/dipdup/codegen.py index d7f312974..fc90154d7 100644 --- a/src/dipdup/codegen.py +++ b/src/dipdup/codegen.py @@ -240,7 +240,7 @@ async def generate_types(self, overwrite_types: bool = False) -> None: input_path = join(root, file) output_path = join(types_root, f'{pascal_to_snake(name)}.py') - if exists and not overwrite_types: + if exists(output_path) and not overwrite_types: continue # NOTE: Skip if the first line starts with "# dipdup: ignore" diff --git a/src/dipdup/config.py b/src/dipdup/config.py index 46880a66f..8fbea72f6 100644 --- a/src/dipdup/config.py +++ b/src/dipdup/config.py @@ -236,8 +236,15 @@ def format_imports(self, package: str) -> Iterator[str]: yield f'from {package} import {cls}' def format_arguments(self) -> Iterator[str]: - for name, cls in self.iter_arguments(): - yield f'{name}: {cls}' + arguments = list(self.iter_arguments()) + i, counter = 0, Counter(name for name, _ in arguments) + + for name, cls in arguments: + if counter[name] > 1: + yield f'{name}_{i}: {cls}' + i += 1 + else: + yield f'{name}: {cls}' def locate_arguments(self) -> Dict[str, Optional[Type]]: kwargs: Dict[str, Optional[Type]] = {} @@ -251,13 +258,13 @@ class PatternConfig(CodegenMixin, ABC): @classmethod def format_storage_import(cls, package: str, module_name: str) -> Tuple[str, str]: storage_cls = f'{snake_to_pascal(module_name)}Storage' - return f'from {package}.types.{module_name}.storage', storage_cls + return f'{package}.types.{module_name}.storage', storage_cls @classmethod def format_parameter_import(cls, package: str, module_name: str, entrypoint: str) -> Tuple[str, str]: entrypoint = entrypoint.lstrip('_') parameter_cls = f'{snake_to_pascal(entrypoint)}Parameter' - return f'from {package}.types.{module_name}.parameter.{pascal_to_snake(entrypoint)}', parameter_cls + return f'{package}.types.{module_name}.parameter.{pascal_to_snake(entrypoint)}', parameter_cls @classmethod def format_origination_argument(cls, module_name: str, optional: bool) -> Tuple[str, str]: @@ -396,7 +403,7 @@ def iter_imports(self, package: str) -> Iterator[Tuple[str, str]]: return module_name = self.destination_contract_config.module_name - yield 'from dipdup.models', 'Transaction' + yield 'dipdup.models', 'Transaction' yield self.format_parameter_import(package, module_name, self.entrypoint) yield self.format_storage_import(package, module_name) @@ -459,7 +466,7 @@ def iter_imports(self, package: str) -> Iterator[Tuple[str, str]]: module_name = self.originated_contract_config.module_name else: raise ConfigurationError('Origination pattern must have at least one of `source`, `similar_to`, `originated_contract` fields') - yield 'from dipdup.models', 'Origination' + yield 'dipdup.models', 'Origination' yield self.format_storage_import(package, module_name) def iter_arguments(self) -> Iterator[Tuple[str, str]]: @@ -666,13 +673,13 @@ def __post_init_post_parse__(self): def format_key_import(cls, package: str, module_name: str, path: str) -> Tuple[str, str]: key_cls = f'{snake_to_pascal(module_name)}Key' key_module = f'{path}_key' - return f'from {package}.types.{module_name}.big_map.{key_module}', key_cls + return f'{package}.types.{module_name}.big_map.{key_module}', key_cls @classmethod def format_value_import(cls, package: str, module_name: str, path: str) -> Tuple[str, str]: value_cls = f'{snake_to_pascal(module_name)}Value' value_module = f'{path}_value' - return f'from {package}.types.{module_name}.big_map.{value_module}', value_cls + return f'{package}.types.{module_name}.big_map.{value_module}', value_cls @classmethod def format_big_map_diff_argument(cls, module_name: str) -> Tuple[str, str]: