Skip to content

fix: set default types #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/codegen/sdk/core/class_definition.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 17 additions & 19 deletions src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]

Expand Down
10 changes: 6 additions & 4 deletions src/codegen/sdk/core/detached_symbols/decorator.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,15 +15,15 @@
if TYPE_CHECKING:
from tree_sitter import Node as TSNode

from codegen.sdk.core.class_definition import Class

Check failure on line 18 in src/codegen/sdk/core/detached_symbols/decorator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Module "codegen.sdk.core.class_definition" has no attribute "Class" [attr-defined]
from codegen.sdk.core.detached_symbols.function_call import FunctionCall
from codegen.sdk.core.detached_symbols.parameter import Parameter
from codegen.sdk.core.function import Function

Check failure on line 21 in src/codegen/sdk/core/detached_symbols/decorator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Module "codegen.sdk.core.function" has no attribute "Function"; maybe "FunctionCall"? [attr-defined]


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
Expand All @@ -30,7 +32,7 @@

def __init__(self, ts_node: TSNode, parent: TClass | TFunction) -> None:
super().__init__(ts_node, parent.file_node_id, parent.ctx, parent)
self._name_node = self._parse_expression(self._get_name_node(), default=Name)

Check failure on line 35 in src/codegen/sdk/core/detached_symbols/decorator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Expression[Decorator[TClass, TFunction, TParameter]]", variable has type "Name[Any] | ChainedAttribute[Any, Any, Any] | DefinedName[Any] | None") [assignment]

@abstractmethod
@reader
Expand Down
29 changes: 15 additions & 14 deletions src/codegen/sdk/core/function.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,33 +25,28 @@
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
from codegen.sdk.core.statements.return_statement import ReturnStatement
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
class Function(

Check failure on line 44 in src/codegen/sdk/core/function.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot determine type of "code_block" in base class "Symbol" [misc]
SupportsGenerics[TType],
HasBlock[TCodeBlock, TDecorator],
Callable[TParameter, TType],
Chainable,
Generic[TFunction, TDecorator, TCodeBlock, TParameter, TType],
Generic[TDecorator, TCodeBlock, TParameter, TType],
):
"""Abstract representation of a Function.

Expand Down Expand Up @@ -125,7 +126,7 @@
Returns:
bool: True if the function is a constructor method of a class, False otherwise.
"""
return self.is_method and self.name == self.parent_class.constructor_keyword

Check failure on line 129 in src/codegen/sdk/core/function.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "None" of "Any | None" has no attribute "constructor_keyword" [union-attr]

@property
def is_async(self) -> bool:
Expand All @@ -145,13 +146,13 @@

for symbol in self.valid_symbol_names:
if symbol.name == name and (start_byte is None or (symbol.start_byte if isinstance(symbol, Class | Function) else symbol.end_byte) <= start_byte):
return symbol

Check failure on line 149 in src/codegen/sdk/core/function.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "Importable[Any]", expected "Symbol[Any, Any] | Import[Any] | WildcardImport[Any] | None") [return-value]
return super().resolve_name(name, start_byte)

@cached_property
@noapidoc
def valid_symbol_names(self) -> list[Importable]:
return sort_editables(self.parameters.symbols + self.descendant_symbols, reverse=True)

Check failure on line 155 in src/codegen/sdk/core/function.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "list[TParameter]" of "SymbolGroup[TParameter, Function[TDecorator, TCodeBlock, TParameter, TType]] | list[TParameter]" has no attribute "symbols" [union-attr]

Check failure on line 155 in src/codegen/sdk/core/function.py

View workflow job for this annotation

GitHub Actions / mypy

error: No overload variant of "__add__" of "list" matches argument type "Sequence[Importable[Any]]" [operator]

# Faster implementation which uses more memory
# @noapidoc
Expand Down Expand Up @@ -205,19 +206,19 @@
Returns:
list[ReturnStatement]: A list of all return statements found within the function's body.
"""
return self.code_block.get_statements(statement_type=StatementType.RETURN_STATEMENT)

Check failure on line 209 in src/codegen/sdk/core/function.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot determine type of "code_block" [has-type]

@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__)]

Check failure on line 221 in src/codegen/sdk/core/function.py

View workflow job for this annotation

GitHub Actions / mypy

error: Cannot determine type of "code_block" [has-type]
return functions

####################################################################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/sdk/python/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/sdk/typescript/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down