Skip to content

Commit bab8220

Browse files
eacodegenbagel897
andauthored
fix: set default types (#536)
Co-authored-by: bagel897 <[email protected]>
1 parent a970656 commit bab8220

File tree

6 files changed

+48
-45
lines changed

6 files changed

+48
-45
lines changed

src/codegen/sdk/core/class_definition.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

33
from abc import abstractmethod
4-
from typing import TYPE_CHECKING, Generic, Literal, Self, TypeVar, overload, override
4+
from typing import TYPE_CHECKING, Generic, Literal, Self, overload, override
5+
6+
from typing_extensions import TypeVar
57

68
from codegen.sdk._proxy import proxy_property
79
from codegen.sdk.core.autocommit import commiter, reader, writer
@@ -42,11 +44,11 @@
4244
logger = logging.getLogger(__name__)
4345

4446

45-
TFunction = TypeVar("TFunction", bound="Function")
46-
TDecorator = TypeVar("TDecorator", bound="Decorator")
47-
TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock")
48-
TParameter = TypeVar("TParameter", bound="Parameter")
49-
TType = TypeVar("TType", bound="Type")
47+
TFunction = TypeVar("TFunction", bound="Function", default="Function")
48+
TDecorator = TypeVar("TDecorator", bound="Decorator", default="Decorator")
49+
TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock", default="CodeBlock")
50+
TParameter = TypeVar("TParameter", bound="Parameter", default="Parameter")
51+
TType = TypeVar("TType", bound="Type", default="Type")
5052

5153

5254
@apidoc

src/codegen/sdk/core/codebase.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from contextlib import contextmanager
1010
from functools import cached_property
1111
from pathlib import Path
12-
from typing import TYPE_CHECKING, Generic, Literal, TypeVar, Unpack, overload
12+
from typing import Generic, Literal, Unpack, overload
1313

1414
import plotly.graph_objects as go
1515
import rich.repr
@@ -19,7 +19,7 @@
1919
from github.PullRequest import PullRequest
2020
from networkx import Graph
2121
from rich.console import Console
22-
from typing_extensions import deprecated
22+
from typing_extensions import TypeVar, deprecated
2323

2424
from codegen.git.repo_operator.local_repo_operator import LocalRepoOperator
2525
from codegen.git.repo_operator.remote_repo_operator import RemoteRepoOperator
@@ -44,6 +44,7 @@
4444
from codegen.sdk.core.detached_symbols.code_block import CodeBlock
4545
from codegen.sdk.core.detached_symbols.parameter import Parameter
4646
from codegen.sdk.core.directory import Directory
47+
from codegen.sdk.core.export import Export
4748
from codegen.sdk.core.external_module import ExternalModule
4849
from codegen.sdk.core.file import File, SourceFile
4950
from codegen.sdk.core.function import Function
@@ -84,27 +85,24 @@
8485
from codegen.shared.performance.stopwatch_utils import stopwatch
8586
from codegen.visualizations.visualization_manager import VisualizationManager
8687

87-
if TYPE_CHECKING:
88-
from codegen.sdk.core.export import Export
89-
9088
logger = logging.getLogger(__name__)
9189
MAX_LINES = 10000 # Maximum number of lines of text allowed to be logged
9290

9391

94-
TSourceFile = TypeVar("TSourceFile", bound="SourceFile")
95-
TDirectory = TypeVar("TDirectory", bound="Directory")
96-
TSymbol = TypeVar("TSymbol", bound="Symbol")
97-
TClass = TypeVar("TClass", bound="Class")
98-
TFunction = TypeVar("TFunction", bound="Function")
99-
TImport = TypeVar("TImport", bound="Import")
100-
TGlobalVar = TypeVar("TGlobalVar", bound="Assignment")
101-
TInterface = TypeVar("TInterface", bound="Interface")
102-
TTypeAlias = TypeVar("TTypeAlias", bound="TypeAlias")
103-
TParameter = TypeVar("TParameter", bound="Parameter")
104-
TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock")
105-
TExport = TypeVar("TExport", bound="Export")
106-
TSGlobalVar = TypeVar("TSGlobalVar", bound="Assignment")
107-
PyGlobalVar = TypeVar("PyGlobalVar", bound="Assignment")
92+
TSourceFile = TypeVar("TSourceFile", bound="SourceFile", default=SourceFile)
93+
TDirectory = TypeVar("TDirectory", bound="Directory", default=Directory)
94+
TSymbol = TypeVar("TSymbol", bound="Symbol", default=Symbol)
95+
TClass = TypeVar("TClass", bound="Class", default=Class)
96+
TFunction = TypeVar("TFunction", bound="Function", default=Function)
97+
TImport = TypeVar("TImport", bound="Import", default=Import)
98+
TGlobalVar = TypeVar("TGlobalVar", bound="Assignment", default=Assignment)
99+
TInterface = TypeVar("TInterface", bound="Interface", default=Interface)
100+
TTypeAlias = TypeVar("TTypeAlias", bound="TypeAlias", default=TypeAlias)
101+
TParameter = TypeVar("TParameter", bound="Parameter", default=Parameter)
102+
TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock", default=CodeBlock)
103+
TExport = TypeVar("TExport", bound="Export", default=Export)
104+
TSGlobalVar = TypeVar("TSGlobalVar", bound="Assignment", default=Assignment)
105+
PyGlobalVar = TypeVar("PyGlobalVar", bound="Assignment", default=Assignment)
108106
TSDirectory = Directory[TSFile, TSSymbol, TSImportStatement, TSGlobalVar, TSClass, TSFunction, TSImport]
109107
PyDirectory = Directory[PyFile, PySymbol, PyImportStatement, PyGlobalVar, PyClass, PyFunction, PyImport]
110108

src/codegen/sdk/core/detached_symbols/decorator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

33
from abc import abstractmethod
4-
from typing import TYPE_CHECKING, Generic, TypeVar
4+
from typing import TYPE_CHECKING, Generic
5+
6+
from typing_extensions import TypeVar
57

68
from codegen.sdk.core.autocommit import reader
79
from codegen.sdk.core.dataclasses.usage import UsageKind
@@ -19,9 +21,9 @@
1921
from codegen.sdk.core.function import Function
2022

2123

22-
TClass = TypeVar("TClass", bound="Class")
23-
TFunction = TypeVar("TFunction", bound="Function")
24-
TParameter = TypeVar("TParameter", bound="Parameter")
24+
TClass = TypeVar("TClass", bound="Class", default="Class")
25+
TFunction = TypeVar("TFunction", bound="Function", default="Function")
26+
TParameter = TypeVar("TParameter", bound="Parameter", default="Parameter")
2527

2628

2729
@apidoc

src/codegen/sdk/core/function.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
from __future__ import annotations
22

33
from abc import abstractmethod
4-
from typing import TYPE_CHECKING, Generic, Self, TypeVar, override
4+
from typing import TYPE_CHECKING, Generic, Self, override
5+
6+
from typing_extensions import TypeVar
57

68
from codegen.sdk.codebase.resolution_stack import ResolutionStack
79
from codegen.sdk.core.autocommit import reader, writer
10+
from codegen.sdk.core.detached_symbols.code_block import CodeBlock
11+
from codegen.sdk.core.detached_symbols.decorator import Decorator
12+
from codegen.sdk.core.detached_symbols.parameter import Parameter
13+
from codegen.sdk.core.expressions.type import Type
814
from codegen.sdk.core.interfaces.callable import Callable
915
from codegen.sdk.core.interfaces.chainable import Chainable
1016
from codegen.sdk.core.interfaces.has_block import HasBlock
@@ -19,24 +25,19 @@
1925
if TYPE_CHECKING:
2026
from collections.abc import Generator, Sequence
2127

22-
from codegen.sdk.core.detached_symbols.code_block import CodeBlock
23-
from codegen.sdk.core.detached_symbols.decorator import Decorator
2428
from codegen.sdk.core.detached_symbols.function_call import FunctionCall
25-
from codegen.sdk.core.detached_symbols.parameter import Parameter
2629
from codegen.sdk.core.export import Export
27-
from codegen.sdk.core.expressions.type import Type
2830
from codegen.sdk.core.file import File
2931
from codegen.sdk.core.import_resolution import Import, WildcardImport
3032
from codegen.sdk.core.interfaces.importable import Importable
3133
from codegen.sdk.core.statements.return_statement import ReturnStatement
3234
from codegen.sdk.core.symbol import Symbol
3335

3436

35-
TFunction = TypeVar("TFunction", bound="Function")
36-
TDecorator = TypeVar("TDecorator", bound="Decorator")
37-
TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock")
38-
TParameter = TypeVar("TParameter", bound="Parameter")
39-
TType = TypeVar("TType", bound="Type")
37+
TDecorator = TypeVar("TDecorator", bound="Decorator", default=Decorator)
38+
TCodeBlock = TypeVar("TCodeBlock", bound="CodeBlock", default=CodeBlock)
39+
TParameter = TypeVar("TParameter", bound="Parameter", default=Parameter)
40+
TType = TypeVar("TType", bound="Type", default=Type)
4041

4142

4243
@apidoc
@@ -45,7 +46,7 @@ class Function(
4546
HasBlock[TCodeBlock, TDecorator],
4647
Callable[TParameter, TType],
4748
Chainable,
48-
Generic[TFunction, TDecorator, TCodeBlock, TParameter, TType],
49+
Generic[TDecorator, TCodeBlock, TParameter, TType],
4950
):
5051
"""Abstract representation of a Function.
5152
@@ -209,15 +210,15 @@ def return_statements(self) -> list[ReturnStatement]:
209210

210211
@property
211212
@reader
212-
def nested_functions(self) -> list[TFunction]:
213+
def nested_functions(self) -> list[Self]:
213214
"""Returns a list of nested functions defined within this function's code block.
214215
215216
Retrieves all functions that are defined within the current function's body. The functions are sorted by their position in the file.
216217
217218
Returns:
218-
list[TFunction]: A list of Function objects representing nested functions within this function's body, sorted by position in the file.
219+
list[Self]: A list of Function objects representing nested functions within this function's body, sorted by position in the file.
219220
"""
220-
functions = [m.symbol for m in self.code_block.symbol_statements if isinstance(m.symbol, Function)]
221+
functions = [m.symbol for m in self.code_block.symbol_statements if isinstance(m.symbol, self.__class__)]
221222
return functions
222223

223224
####################################################################################################################

src/codegen/sdk/python/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
@py_apidoc
34-
class PyFunction(Function["PyFunction", PyDecorator, PyCodeBlock, PyParameter, PyType], PyHasBlock, PySymbol):
34+
class PyFunction(Function[PyDecorator, PyCodeBlock, PyParameter, PyType], PyHasBlock, PySymbol):
3535
"""Extends Function for Python codebases."""
3636

3737
_decorated_node: TSNode | None

src/codegen/sdk/typescript/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636

3737
@ts_apidoc
38-
class TSFunction(Function["TSFunction", TSDecorator, "TSCodeBlock", TSParameter, TSType], TSHasBlock, TSSymbol):
38+
class TSFunction(Function[TSDecorator, "TSCodeBlock", TSParameter, TSType], TSHasBlock, TSSymbol):
3939
"""Representation of a Function in JavaScript/TypeScript"""
4040

4141
@noapidoc

0 commit comments

Comments
 (0)