Skip to content

Commit 65ef228

Browse files
Fix check of existing types, rename arguments with the same name on codegen (#141)
* Fix typos in codegen module * Avoid duplicate arguments * Changelog * Changelog
1 parent 3007015 commit 65ef228

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## 3.0.2 - 2021-09-25
3+
## [unreleased]
44

55
### Added
66

@@ -15,7 +15,9 @@
1515
* Removed unnecessary calls to TzKT API during the partial sync.
1616
* Fixed removal of PostgreSQL extensions (`timescaledb`, `pgcrypto`) by function `truncate_database` triggered on reindex.
1717
* Fixed updating relation between index and head in DB.
18-
* Fixed creation of missing project package
18+
* Fixed creation of missing project package on `init`.
19+
* Fixed invalid handler callbacks generated on `init`.
20+
* Fixed detection of existing types in the project.
1921

2022
## 3.0.1 - 2021-09-24
2123

src/dipdup/codegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async def generate_types(self, overwrite_types: bool = False) -> None:
240240
input_path = join(root, file)
241241
output_path = join(types_root, f'{pascal_to_snake(name)}.py')
242242

243-
if exists and not overwrite_types:
243+
if exists(output_path) and not overwrite_types:
244244
continue
245245

246246
# NOTE: Skip if the first line starts with "# dipdup: ignore"

src/dipdup/config.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,15 @@ def format_imports(self, package: str) -> Iterator[str]:
236236
yield f'from {package} import {cls}'
237237

238238
def format_arguments(self) -> Iterator[str]:
239-
for name, cls in self.iter_arguments():
240-
yield f'{name}: {cls}'
239+
arguments = list(self.iter_arguments())
240+
i, counter = 0, Counter(name for name, _ in arguments)
241+
242+
for name, cls in arguments:
243+
if counter[name] > 1:
244+
yield f'{name}_{i}: {cls}'
245+
i += 1
246+
else:
247+
yield f'{name}: {cls}'
241248

242249
def locate_arguments(self) -> Dict[str, Optional[Type]]:
243250
kwargs: Dict[str, Optional[Type]] = {}
@@ -251,13 +258,13 @@ class PatternConfig(CodegenMixin, ABC):
251258
@classmethod
252259
def format_storage_import(cls, package: str, module_name: str) -> Tuple[str, str]:
253260
storage_cls = f'{snake_to_pascal(module_name)}Storage'
254-
return f'from {package}.types.{module_name}.storage', storage_cls
261+
return f'{package}.types.{module_name}.storage', storage_cls
255262

256263
@classmethod
257264
def format_parameter_import(cls, package: str, module_name: str, entrypoint: str) -> Tuple[str, str]:
258265
entrypoint = entrypoint.lstrip('_')
259266
parameter_cls = f'{snake_to_pascal(entrypoint)}Parameter'
260-
return f'from {package}.types.{module_name}.parameter.{pascal_to_snake(entrypoint)}', parameter_cls
267+
return f'{package}.types.{module_name}.parameter.{pascal_to_snake(entrypoint)}', parameter_cls
261268

262269
@classmethod
263270
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]]:
396403
return
397404

398405
module_name = self.destination_contract_config.module_name
399-
yield 'from dipdup.models', 'Transaction'
406+
yield 'dipdup.models', 'Transaction'
400407
yield self.format_parameter_import(package, module_name, self.entrypoint)
401408
yield self.format_storage_import(package, module_name)
402409

@@ -459,7 +466,7 @@ def iter_imports(self, package: str) -> Iterator[Tuple[str, str]]:
459466
module_name = self.originated_contract_config.module_name
460467
else:
461468
raise ConfigurationError('Origination pattern must have at least one of `source`, `similar_to`, `originated_contract` fields')
462-
yield 'from dipdup.models', 'Origination'
469+
yield 'dipdup.models', 'Origination'
463470
yield self.format_storage_import(package, module_name)
464471

465472
def iter_arguments(self) -> Iterator[Tuple[str, str]]:
@@ -666,13 +673,13 @@ def __post_init_post_parse__(self):
666673
def format_key_import(cls, package: str, module_name: str, path: str) -> Tuple[str, str]:
667674
key_cls = f'{snake_to_pascal(module_name)}Key'
668675
key_module = f'{path}_key'
669-
return f'from {package}.types.{module_name}.big_map.{key_module}', key_cls
676+
return f'{package}.types.{module_name}.big_map.{key_module}', key_cls
670677

671678
@classmethod
672679
def format_value_import(cls, package: str, module_name: str, path: str) -> Tuple[str, str]:
673680
value_cls = f'{snake_to_pascal(module_name)}Value'
674681
value_module = f'{path}_value'
675-
return f'from {package}.types.{module_name}.big_map.{value_module}', value_cls
682+
return f'{package}.types.{module_name}.big_map.{value_module}', value_cls
676683

677684
@classmethod
678685
def format_big_map_diff_argument(cls, module_name: str) -> Tuple[str, str]:

0 commit comments

Comments
 (0)