← 输入框|github.com|刷新
Skip to content

One way to ignore type annotations #2327

Open

Description

@certik
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from lpython import i32

def main0(i: i32):
    x: i32
    x = (2+3)*5+i
    print(x)

if __name__ == "__main__":
   main0(5)

This works with cpython, it will ignore the lpython.py.

Activity

  1. Lavanyajc commented on Sep 17, 2023

    @Lavanyajc

    #2328 Suppose you have a file named lpython.py that defines the i32 type and any other functionality you need. Your lpython.py file could look like this:

    lpython.py

    class i32:
    def init(self, value: int):
    self.value = value

    def __add__(self, other):
        if isinstance(other, i32):
            return i32(self.value + other.value)
        return NotImplemented
    
    def __mul__(self, other):
        if isinstance(other, i32):
            return i32(self.value * other.value)
        return NotImplemented
    
    def __str__(self):
        return str(self.value)
    

    #Now, you can modify your main script to work with the lpython module:

    from future import annotations
    from typing import TYPE_CHECKING

    if TYPE_CHECKING:
    from lpython import i32

    def main0(i: i32):
    x: i32
    x = (i32(2) + i32(3)) * i + i
    print(x)

    if name == "main":
    from lpython import i32 # Import the i32 type from lpython
    main0(i32(5)) # Pass an i32 instance to main0

    i hope its right

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