Skip to content

Allow to initialize nested structs using "Transactions(transactions=[], accounts=[])" #2428

Open

Description

@certik

With the following definitions:

from lpython import dataclass, i64

@dataclass
class TransactionItem:
    account_idx: i64
    commodity: str
    amount: i64

@dataclass
class Transaction:
    date: str
    name: str
    cleared: bool
    comments: str
    items: list[TransactionItem]

@dataclass
class Account:
    name: str

@dataclass
class Transactions:
    transactions: list[Transaction]
    accounts: list[Account]

This does not work:

asr: Transactions = Transactions(transactions=[], accounts=[])

This workaround works:

transactions: list[Transaction] = []
accounts: list[Account] = []
asr: Transactions = Transactions(transactions=transactions, accounts=accounts)

Activity

  1. certik commented on Nov 18, 2023

    @certik
    ContributorAuthor

    The workaround above gives ASR, but fails to compile:

    $ lpython -I. process.py
    ASR verify pass error: ASR verify: The symbol table was not found in the scope of `symtab`.
    
    ASR verify pass error: ASR verify: Struct::m_derived_type 'Transaction' cannot point outside of its symbol table, owner: model
    Internal Compiler Error: Unhandled exception
    Traceback (most recent call last):
      File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 1869
        err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
      File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 828
        res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile);
      File "/Users/ondrej/repos/lpython/src/lpython/python_evaluator.cpp", line 71
        run_fn, infile);
      File "/Users/ondrej/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 8933
        pass_manager.apply_passes(al, &asr, pass_options, diagnostics);
      File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_manager.h", line 305
        apply_passes(al, asr, _passes, pass_options, diagnostics);
      File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_manager.h", line 169
        + passes[i]);
    LCompilersException: Verify failed in the pass: class_constructor
  2. arteevraina commented on Nov 20, 2023

    @arteevraina
    Collaborator

    Hi @certik ,
    I tried to reproduce the above issue for the work around code.

    from lpython import dataclass, i64
    
    @dataclass
    class TransactionItem:
        account_idx: i64
        commodity: str
        amount: i64
    
    @dataclass
    class Transaction:
        date: str
        name: str
        cleared: bool
        comments: str
        items: list[TransactionItem]
    
    @dataclass
    class Account:
        name: str
    
    @dataclass
    class Transactions:
        transactions: list[Transaction]
        accounts: list[Account]
    
    transactions: list[Transaction] = []
    accounts: list[Account] = []
    asr: Transactions = Transactions(transactions=transactions, accounts=accounts)
    

    I am able to reproduce the issue for the workaround code when I use two different modules/files. One contains the definition of the classes and in the other file i import the classes and use the workaround initialization but when I put definition and initialization in a single file I am not able to reproduce it.

  3. certik commented on Nov 20, 2023

    @certik
    ContributorAuthor

    Yes, I was using two different files.

  4. certik commented on Dec 8, 2023

    @certik
    ContributorAuthor

    To fix this, we need to do lfortran/lfortran#2957.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions