docs: add some docstrings
This commit is contained in:
@@ -1,3 +1,9 @@
|
|||||||
|
"""
|
||||||
|
Helper script to generate AST nodes for Midas and Python.
|
||||||
|
|
||||||
|
Takes in simple templates and generates full dataclasses and a visitor interface
|
||||||
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ class HasLocation(Protocol):
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class Location:
|
class Location:
|
||||||
|
"""Information about the location of an AST node"""
|
||||||
|
|
||||||
lineno: int
|
lineno: int
|
||||||
col_offset: int
|
col_offset: int
|
||||||
end_lineno: Optional[int]
|
end_lineno: Optional[int]
|
||||||
@@ -29,6 +31,16 @@ class Location:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def span(start: Location, end: Location) -> Location:
|
def span(start: Location, end: Location) -> Location:
|
||||||
|
"""Create a new location spanning from one location to another
|
||||||
|
|
||||||
|
Args:
|
||||||
|
start (Location): the starting location
|
||||||
|
end (Location): the end location
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Location: a new location spanning from the start of `start`
|
||||||
|
to the end of `end`
|
||||||
|
"""
|
||||||
return Location(
|
return Location(
|
||||||
lineno=start.lineno,
|
lineno=start.lineno,
|
||||||
col_offset=start.col_offset,
|
col_offset=start.col_offset,
|
||||||
|
|||||||
@@ -14,12 +14,15 @@ if TYPE_CHECKING:
|
|||||||
from midas.checker.registry import TypesRegistry
|
from midas.checker.registry import TypesRegistry
|
||||||
|
|
||||||
|
|
||||||
# Hard-coded subtype relationships between builtin types
|
|
||||||
# Circular dependencies and diamond inheritance MUST be avoided
|
|
||||||
BUILTIN_SUBTYPES: dict[str, set[str]] = {
|
BUILTIN_SUBTYPES: dict[str, set[str]] = {
|
||||||
"object": {"float", "list", "dict", "str", "bytes", "tuple"},
|
"object": {"float", "list", "dict", "str", "bytes", "tuple"},
|
||||||
"float": {"int"},
|
"float": {"int"},
|
||||||
}
|
}
|
||||||
|
"""
|
||||||
|
Hard-coded subtype relationships between builtin types
|
||||||
|
|
||||||
|
Circular dependencies and diamond inheritance MUST be avoided
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def define_builtins(reg: TypesRegistry):
|
def define_builtins(reg: TypesRegistry):
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ from midas.utils import TypedAST
|
|||||||
|
|
||||||
|
|
||||||
class TypeChecker:
|
class TypeChecker:
|
||||||
|
"""Type checking dispatcher
|
||||||
|
|
||||||
|
Contains a typer for Midas and one for Python, as well as the types registry
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.types: TypesRegistry = TypesRegistry()
|
self.types: TypesRegistry = TypesRegistry()
|
||||||
self.reporter: Reporter = Reporter()
|
self.reporter: Reporter = Reporter()
|
||||||
|
|||||||
@@ -14,6 +14,15 @@ class DiagnosticType(StrEnum):
|
|||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Diagnostic:
|
class Diagnostic:
|
||||||
|
"""Information about a diagnostic (warning, errors, etc.)
|
||||||
|
|
||||||
|
Holds a location, a diagnostic type and a message.
|
||||||
|
Optionally bound to a file path
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_type_: _description_
|
||||||
|
"""
|
||||||
|
|
||||||
file_path: Optional[str]
|
file_path: Optional[str]
|
||||||
location: Location
|
location: Location
|
||||||
type: DiagnosticType
|
type: DiagnosticType
|
||||||
@@ -21,6 +30,18 @@ class Diagnostic:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def location_str(self) -> str:
|
def location_str(self) -> str:
|
||||||
|
"""The diagnostic type and location as a human readable string
|
||||||
|
|
||||||
|
The location is formatted as "<Type> in <file> from L<start_line>:<start_col> to <end_line>:<end_col>",
|
||||||
|
for example: "Error in /home/user/Desktop/script.py from L12:5 to L12:8"
|
||||||
|
|
||||||
|
If the file is `None`, the "in ..." section is excluded from the result.<br>
|
||||||
|
If the location's end is not specified, the formulation "at L<start_line>:<start_col>" is used.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: _description_
|
||||||
|
"""
|
||||||
|
|
||||||
start_loc: str = f"L{self.location.lineno}:{self.location.col_offset+1}"
|
start_loc: str = f"L{self.location.lineno}:{self.location.col_offset+1}"
|
||||||
end_loc: Optional[str] = ""
|
end_loc: Optional[str] = ""
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user