From 05fca7284fc5bcfef2da714c53f702eb1ae2f249 Mon Sep 17 00:00:00 2001 From: bagel897 Date: Tue, 18 Feb 2025 15:12:46 -0800 Subject: [PATCH 1/2] update type hints --- src/codegen/sdk/core/codebase.py | 36 +++++++++---------- .../sdk/core/detached_symbols/decorator.py | 10 +++--- src/codegen/sdk/core/function.py | 29 +++++++-------- src/codegen/sdk/python/function.py | 2 +- src/codegen/sdk/typescript/function.py | 2 +- 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/codegen/sdk/core/codebase.py b/src/codegen/sdk/core/codebase.py index 0de1c1f2e..d4c347dfc 100644 --- a/src/codegen/sdk/core/codebase.py +++ b/src/codegen/sdk/core/codebase.py @@ -9,7 +9,7 @@ from contextlib import contextmanager from functools import cached_property from pathlib import Path -from typing import TYPE_CHECKING, Generic, Literal, TypeVar, Unpack, overload +from typing import Generic, Literal, Unpack, overload import plotly.graph_objects as go import rich.repr @@ -19,7 +19,7 @@ from github.PullRequest import PullRequest from networkx import Graph from rich.console import Console -from typing_extensions import deprecated +from typing_extensions import TypeVar, deprecated from codegen.git.repo_operator.local_repo_operator import LocalRepoOperator from codegen.git.repo_operator.remote_repo_operator import RemoteRepoOperator @@ -44,6 +44,7 @@ from codegen.sdk.core.detached_symbols.code_block import CodeBlock from codegen.sdk.core.detached_symbols.parameter import Parameter from codegen.sdk.core.directory import Directory +from codegen.sdk.core.export import Export from codegen.sdk.core.external_module import ExternalModule from codegen.sdk.core.file import File, SourceFile from codegen.sdk.core.function import Function @@ -84,27 +85,24 @@ from codegen.shared.performance.stopwatch_utils import stopwatch from codegen.visualizations.visualization_manager import VisualizationManager -if TYPE_CHECKING: - from codegen.sdk.core.export import Export - logger = logging.getLogger(__name__) MAX_LINES = 10000 # Maximum number of lines of text allowed to be logged -TSourceFile = TypeVar("TSourceFile", bound="SourceFile") -TDirectory = TypeVar("TDirectory", bound="Directory") -TSymbol = TypeVar("TSymbol", bound="Symbol") -TClass = TypeVar("TClass", bound="Class") -TFunction = TypeVar("TFunction", bound="Function") -TImport = TypeVar("TImport", bound="Import") -TGlobalVar = TypeVar("TGlobalVar", bound="Assignment") -TInterface = TypeVar("TInterface", bound="Interface") -TTypeAlias = TypeVar("TTypeAlias", bound="TypeAlias") -TParameter = TypeVar("TParameter", bound="Parameter") -TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock") -TExport = TypeVar("TExport", bound="Export") -TSGlobalVar = TypeVar("TSGlobalVar", bound="Assignment") -PyGlobalVar = TypeVar("PyGlobalVar", bound="Assignment") +TSourceFile = TypeVar("TSourceFile", bound="SourceFile", default=SourceFile) +TDirectory = TypeVar("TDirectory", bound="Directory", default=Directory) +TSymbol = TypeVar("TSymbol", bound="Symbol", default=Symbol) +TClass = TypeVar("TClass", bound="Class", default=Class) +TFunction = TypeVar("TFunction", bound="Function", default=Function) +TImport = TypeVar("TImport", bound="Import", default=Import) +TGlobalVar = TypeVar("TGlobalVar", bound="Assignment", default=Assignment) +TInterface = TypeVar("TInterface", bound="Interface", default=Interface) +TTypeAlias = TypeVar("TTypeAlias", bound="TypeAlias", default=TypeAlias) +TParameter = TypeVar("TParameter", bound="Parameter", default=Parameter) +TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock", default=CodeBlock) +TExport = TypeVar("TExport", bound="Export", default=Export) +TSGlobalVar = TypeVar("TSGlobalVar", bound="Assignment", default=Assignment) +PyGlobalVar = TypeVar("PyGlobalVar", bound="Assignment", default=Assignment) TSDirectory = Directory[TSFile, TSSymbol, TSImportStatement, TSGlobalVar, TSClass, TSFunction, TSImport] PyDirectory = Directory[PyFile, PySymbol, PyImportStatement, PyGlobalVar, PyClass, PyFunction, PyImport] diff --git a/src/codegen/sdk/core/detached_symbols/decorator.py b/src/codegen/sdk/core/detached_symbols/decorator.py index ee23d0f80..5a756b392 100644 --- a/src/codegen/sdk/core/detached_symbols/decorator.py +++ b/src/codegen/sdk/core/detached_symbols/decorator.py @@ -1,7 +1,9 @@ from __future__ import annotations from abc import abstractmethod -from typing import TYPE_CHECKING, Generic, TypeVar +from typing import TYPE_CHECKING, Generic + +from typing_extensions import TypeVar from codegen.sdk.core.autocommit import reader from codegen.sdk.core.dataclasses.usage import UsageKind @@ -19,9 +21,9 @@ from codegen.sdk.core.function import Function -TClass = TypeVar("TClass", bound="Class") -TFunction = TypeVar("TFunction", bound="Function") -TParameter = TypeVar("TParameter", bound="Parameter") +TClass = TypeVar("TClass", bound="Class", default="Class") +TFunction = TypeVar("TFunction", bound="Function", default="Function") +TParameter = TypeVar("TParameter", bound="Parameter", default="Parameter") @apidoc diff --git a/src/codegen/sdk/core/function.py b/src/codegen/sdk/core/function.py index 20b400cc6..21aa3c1df 100644 --- a/src/codegen/sdk/core/function.py +++ b/src/codegen/sdk/core/function.py @@ -1,10 +1,16 @@ from __future__ import annotations from abc import abstractmethod -from typing import TYPE_CHECKING, Generic, Self, TypeVar, override +from typing import TYPE_CHECKING, Generic, Self, override + +from typing_extensions import TypeVar from codegen.sdk.codebase.resolution_stack import ResolutionStack from codegen.sdk.core.autocommit import reader, writer +from codegen.sdk.core.detached_symbols.code_block import CodeBlock +from codegen.sdk.core.detached_symbols.decorator import Decorator +from codegen.sdk.core.detached_symbols.parameter import Parameter +from codegen.sdk.core.expressions.type import Type from codegen.sdk.core.interfaces.callable import Callable from codegen.sdk.core.interfaces.chainable import Chainable from codegen.sdk.core.interfaces.has_block import HasBlock @@ -19,12 +25,8 @@ if TYPE_CHECKING: from collections.abc import Generator, Sequence - from codegen.sdk.core.detached_symbols.code_block import CodeBlock - from codegen.sdk.core.detached_symbols.decorator import Decorator from codegen.sdk.core.detached_symbols.function_call import FunctionCall - from codegen.sdk.core.detached_symbols.parameter import Parameter from codegen.sdk.core.export import Export - from codegen.sdk.core.expressions.type import Type from codegen.sdk.core.file import File from codegen.sdk.core.import_resolution import Import, WildcardImport from codegen.sdk.core.interfaces.importable import Importable @@ -32,11 +34,10 @@ from codegen.sdk.core.symbol import Symbol -TFunction = TypeVar("TFunction", bound="Function") -TDecorator = TypeVar("TDecorator", bound="Decorator") -TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock") -TParameter = TypeVar("TParameter", bound="Parameter") -TType = TypeVar("TType", bound="Type") +TDecorator = TypeVar("TDecorator", bound="Decorator", default=Decorator) +TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock", default=CodeBlock) +TParameter = TypeVar("TParameter", bound="Parameter", default=Parameter) +TType = TypeVar("TType", bound="Type", default=Type) @apidoc @@ -45,7 +46,7 @@ class Function( HasBlock[TCodeBlock, TDecorator], Callable[TParameter, TType], Chainable, - Generic[TFunction, TDecorator, TCodeBlock, TParameter, TType], + Generic[TDecorator, TCodeBlock, TParameter, TType], ): """Abstract representation of a Function. @@ -209,15 +210,15 @@ def return_statements(self) -> list[ReturnStatement]: @property @reader - def nested_functions(self) -> list[TFunction]: + def nested_functions(self) -> list[Self]: """Returns a list of nested functions defined within this function's code block. Retrieves all functions that are defined within the current function's body. The functions are sorted by their position in the file. Returns: - list[TFunction]: A list of Function objects representing nested functions within this function's body, sorted by position in the file. + list[Self]: A list of Function objects representing nested functions within this function's body, sorted by position in the file. """ - functions = [m.symbol for m in self.code_block.symbol_statements if isinstance(m.symbol, Function)] + functions = [m.symbol for m in self.code_block.symbol_statements if isinstance(m.symbol, self.__class__)] return functions #################################################################################################################### diff --git a/src/codegen/sdk/python/function.py b/src/codegen/sdk/python/function.py index dd8720317..1b77265f2 100644 --- a/src/codegen/sdk/python/function.py +++ b/src/codegen/sdk/python/function.py @@ -31,7 +31,7 @@ @py_apidoc -class PyFunction(Function["PyFunction", PyDecorator, PyCodeBlock, PyParameter, PyType], PyHasBlock, PySymbol): +class PyFunction(Function[PyDecorator, PyCodeBlock, PyParameter, PyType], PyHasBlock, PySymbol): """Extends Function for Python codebases.""" _decorated_node: TSNode | None diff --git a/src/codegen/sdk/typescript/function.py b/src/codegen/sdk/typescript/function.py index 81a52f1cf..4c1b5ff60 100644 --- a/src/codegen/sdk/typescript/function.py +++ b/src/codegen/sdk/typescript/function.py @@ -35,7 +35,7 @@ @ts_apidoc -class TSFunction(Function["TSFunction", TSDecorator, "TSCodeBlock", TSParameter, TSType], TSHasBlock, TSSymbol): +class TSFunction(Function[TSDecorator, "TSCodeBlock", TSParameter, TSType], TSHasBlock, TSSymbol): """Representation of a Function in JavaScript/TypeScript""" @noapidoc From ab32d44c5ce272e59799ec50486996235a7ec713 Mon Sep 17 00:00:00 2001 From: bagel897 Date: Tue, 18 Feb 2025 15:13:05 -0800 Subject: [PATCH 2/2] update type hints --- src/codegen/sdk/core/class_definition.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/codegen/sdk/core/class_definition.py b/src/codegen/sdk/core/class_definition.py index 623986bb6..ebc8df467 100644 --- a/src/codegen/sdk/core/class_definition.py +++ b/src/codegen/sdk/core/class_definition.py @@ -1,7 +1,9 @@ from __future__ import annotations from abc import abstractmethod -from typing import TYPE_CHECKING, Generic, Literal, Self, TypeVar, overload, override +from typing import TYPE_CHECKING, Generic, Literal, Self, overload, override + +from typing_extensions import TypeVar from codegen.sdk._proxy import proxy_property from codegen.sdk.core.autocommit import commiter, reader, writer @@ -42,11 +44,11 @@ logger = logging.getLogger(__name__) -TFunction = TypeVar("TFunction", bound="Function") -TDecorator = TypeVar("TDecorator", bound="Decorator") -TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock") -TParameter = TypeVar("TParameter", bound="Parameter") -TType = TypeVar("TType", bound="Type") +TFunction = TypeVar("TFunction", bound="Function", default="Function") +TDecorator = TypeVar("TDecorator", bound="Decorator", default="Decorator") +TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock", default="CodeBlock") +TParameter = TypeVar("TParameter", bound="Parameter", default="Parameter") +TType = TypeVar("TType", bound="Type", default="Type") @apidoc