typedframes 0.14.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
typedframes/mypy.py ADDED
@@ -0,0 +1,140 @@
1
+ """Mypy plugin for typedframes column checking."""
2
+
3
+ import json
4
+ import tomllib
5
+ from collections.abc import Callable
6
+ from pathlib import Path
7
+ from typing import TYPE_CHECKING, Any
8
+
9
+ from mypy.plugin import MethodContext, Plugin
10
+
11
+ from typedframes._binary import BinaryNotFoundError, run_binary
12
+
13
+ if TYPE_CHECKING:
14
+ from mypy.types import Type
15
+
16
+
17
+ def get_project_root(start_path: Path) -> Path:
18
+ """Find the project root by looking for pyproject.toml."""
19
+ current = start_path.resolve()
20
+ if current.is_file():
21
+ current = current.parent
22
+ for parent in [current, *list(current.parents)]:
23
+ if (parent / "pyproject.toml").exists():
24
+ return parent
25
+ return current
26
+
27
+
28
+ def is_enabled(project_root: Path) -> bool:
29
+ """Check if the checker is enabled in pyproject.toml."""
30
+ config_path = project_root / "pyproject.toml"
31
+ if not config_path.exists():
32
+ return True
33
+ try:
34
+ with config_path.open("rb") as f:
35
+ config = tomllib.load(f)
36
+ enabled = config.get("tool", {}).get("typedframes", {}).get("enabled", True)
37
+ return bool(enabled)
38
+ except (OSError, tomllib.TOMLDecodeError):
39
+ return True
40
+
41
+
42
+ class CheckerNotFoundError(Exception):
43
+ """Raised when the typedframes checker cannot be found or executed."""
44
+
45
+
46
+ class CheckerFailedError(Exception):
47
+ """Raised when the typedframes checker ran but did not produce a result."""
48
+
49
+
50
+ class TypedFramesPlugin(Plugin):
51
+ """Mypy plugin to integrate the typedframes Rust checker."""
52
+
53
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
54
+ """Initialize the plugin."""
55
+ super().__init__(*args, **kwargs)
56
+ self._errors_by_root: dict[str, dict[str, list[dict[str, Any]]]] = {}
57
+ # The hook runs for every subscript in a file, so a file's answer is worked out once.
58
+ self._errors_by_file: dict[str, list[dict[str, Any]]] = {}
59
+
60
+ def _project_errors(self, project_root: Path) -> dict[str, list[dict[str, Any]]]:
61
+ """Every file's diagnostics for a project, by file path: the checker runs once per project root.
62
+
63
+ Without a `pyproject.toml` a file's own directory is its root, so a root inside one that was
64
+ already checked is served from that run.
65
+ """
66
+ key = str(project_root)
67
+ for checked, errors in self._errors_by_root.items():
68
+ if project_root.is_relative_to(checked):
69
+ return errors
70
+ try:
71
+ completed = run_binary(["check", key, "--output-format", "json"], capture=True)
72
+ except BinaryNotFoundError as e:
73
+ msg = (
74
+ "typedframes checker executable not found. "
75
+ "Ensure the package is properly installed with 'uv add typedframes' "
76
+ "or build from source with 'uv run inv build'."
77
+ )
78
+ raise CheckerNotFoundError(msg) from e
79
+ if completed.returncode != 0:
80
+ msg = f"typedframes check exited with status {completed.returncode}: {completed.stderr.strip()}"
81
+ raise CheckerFailedError(msg)
82
+ by_file: dict[str, list[dict[str, Any]]] = {}
83
+ for error in json.loads(completed.stdout)["errors"]:
84
+ by_file.setdefault(error["file"], []).append(error)
85
+ self._errors_by_root[key] = by_file
86
+ return by_file
87
+
88
+ def _run_checker(self, file_path: str) -> list[dict[str, Any]]:
89
+ """The checker's diagnostics for the given file."""
90
+ if file_path in self._errors_by_file:
91
+ return self._errors_by_file[file_path]
92
+
93
+ if not file_path or "site-packages" in file_path or file_path.endswith(".pyi"):
94
+ return []
95
+
96
+ project_root = get_project_root(Path(file_path))
97
+ if is_enabled(project_root):
98
+ errors = self._project_errors(project_root).get(str(Path(file_path).resolve()), [])
99
+ else:
100
+ errors = []
101
+ self._errors_by_file[file_path] = errors
102
+ return errors
103
+
104
+ def get_method_hook(
105
+ self,
106
+ fullname: str,
107
+ ) -> Callable[[MethodContext], "Type"] | None:
108
+ """Return a hook for DataFrame access methods."""
109
+ # Target common methods where column access happens
110
+ if fullname.endswith((".__getitem__", ".__setitem__")):
111
+ return self.check_column_access
112
+ return None
113
+
114
+ def check_column_access(self, context: MethodContext) -> "Type":
115
+ """Check if the DataFrame column access is valid."""
116
+ file_path = getattr(context.api, "path", None)
117
+ if not file_path:
118
+ return context.default_return_type
119
+
120
+ errors = self._run_checker(file_path)
121
+ line = context.context.line
122
+
123
+ matched = False
124
+ for err in errors:
125
+ if err["line"] == line:
126
+ context.api.fail(err["message"], context.context)
127
+ matched = True
128
+
129
+ if not matched:
130
+ # Try fuzzy matching for multi-line expressions
131
+ for err in errors:
132
+ if abs(err["line"] - line) <= 1:
133
+ context.api.fail(err["message"], context.context)
134
+
135
+ return context.default_return_type
136
+
137
+
138
+ def plugin(_version: str) -> type[TypedFramesPlugin]:
139
+ """Entry point for the mypy plugin."""
140
+ return TypedFramesPlugin
typedframes/pandera.py ADDED
@@ -0,0 +1,79 @@
1
+ """Pandera integration for typedframes schemas."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Any
6
+
7
+ if TYPE_CHECKING:
8
+ import pandera as pa
9
+
10
+ from .base_schema import BaseSchema
11
+
12
+
13
+ def _map_dtype(python_type: type) -> type | None:
14
+ """Map a Python type to the corresponding pandera-compatible type.
15
+
16
+ Args:
17
+ python_type: The Python type from a Column or ColumnSet definition.
18
+
19
+ Returns:
20
+ The type to pass to pandera, or None if no type check should be applied.
21
+
22
+ """
23
+ if python_type is Any:
24
+ return None
25
+ return python_type
26
+
27
+
28
+ def to_pandera_schema(schema: type[BaseSchema]) -> pa.DataFrameSchema:
29
+ """Convert a typedframes BaseSchema to a pandera DataFrameSchema.
30
+
31
+ Maps Column and ColumnSet definitions to pandera Column objects,
32
+ enabling runtime validation using the same schema definitions
33
+ used for static analysis.
34
+
35
+ Args:
36
+ schema: A BaseSchema subclass to convert.
37
+
38
+ Returns:
39
+ A pandera DataFrameSchema with columns matching the input schema.
40
+
41
+ Raises:
42
+ MissingDependencyError: If pandera is not installed.
43
+
44
+ Example:
45
+ class UserData(BaseSchema):
46
+ user_id = Column(type=int)
47
+ email = Column(type=str)
48
+ age = Column(type=int, nullable=True)
49
+
50
+ pandera_schema = to_pandera_schema(UserData)
51
+ validated_df = pandera_schema.validate(df)
52
+
53
+ """
54
+ try:
55
+ import pandera as pa
56
+ except ImportError:
57
+ from .missing_dependency_error import MissingDependencyError
58
+
59
+ package = "pandera"
60
+ raise MissingDependencyError(package, "to_pandera_schema") from None
61
+
62
+ columns: dict[str, pa.Column] = {}
63
+
64
+ for col in schema.columns().values():
65
+ dtype = _map_dtype(col.type)
66
+ columns[col.column_name] = pa.Column(dtype=dtype, nullable=col.nullable)
67
+
68
+ for cs in schema.column_sets().values():
69
+ dtype = _map_dtype(cs.type)
70
+ if cs.regex:
71
+ for pattern in cs.members:
72
+ columns[pattern] = pa.Column(dtype=dtype, nullable=False, regex=True)
73
+ else:
74
+ for member in cs.members:
75
+ columns[member] = pa.Column(dtype=dtype, nullable=False)
76
+
77
+ strict = not schema.allow_extra_columns
78
+
79
+ return pa.DataFrameSchema(columns=columns, strict=strict)
typedframes/py.typed ADDED
File without changes
@@ -0,0 +1,101 @@
1
+ """Schema algebra operations for combining and transforming schemas."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ from .column import Column
8
+ from .column_set import ColumnSet
9
+
10
+ if TYPE_CHECKING:
11
+ from .base_schema import BaseSchema
12
+
13
+
14
+ class SchemaConflictError(TypeError):
15
+ """Raised when combining schemas with conflicting column types."""
16
+
17
+ def __init__(self, column_name: str, type_a: type, schema_a: str, type_b: type, schema_b: str) -> None:
18
+ """Initialize with details about the conflicting column and types."""
19
+ self.column_name = column_name
20
+ self.type_a = type_a
21
+ self.type_b = type_b
22
+ super().__init__(
23
+ f"Column '{column_name}' has conflicting types: "
24
+ f"{type_a.__name__} ({schema_a}) vs {type_b.__name__} ({schema_b})"
25
+ )
26
+
27
+
28
+ def combine_schemas(
29
+ schema_a: type[BaseSchema],
30
+ schema_b: type[BaseSchema],
31
+ name: str | None = None,
32
+ ) -> type[BaseSchema]:
33
+ """
34
+ Combine two schemas into a new schema with all columns from both.
35
+
36
+ This is the functional equivalent of SchemaA + SchemaB.
37
+
38
+ Args:
39
+ schema_a: First schema class.
40
+ schema_b: Second schema class.
41
+ name: Optional name for the combined schema. Defaults to "SchemaA_SchemaB".
42
+
43
+ Returns:
44
+ A new schema class with columns from both schemas.
45
+
46
+ Raises:
47
+ SchemaConflictError: If both schemas have a column with the same name but different types.
48
+
49
+ """
50
+ from .base_schema import BaseSchema
51
+
52
+ combined_name = name or f"{schema_a.__name__}_{schema_b.__name__}"
53
+
54
+ attrs: dict = {}
55
+
56
+ for col_name, col in schema_a.columns().items():
57
+ attrs[col_name] = Column(
58
+ type=col.type,
59
+ alias=col.alias,
60
+ nullable=col.nullable,
61
+ description=col.description,
62
+ )
63
+
64
+ for col_name, col in schema_b.columns().items():
65
+ if col_name in attrs:
66
+ existing = attrs[col_name]
67
+ if existing.type != col.type:
68
+ raise SchemaConflictError(col_name, existing.type, schema_a.__name__, col.type, schema_b.__name__)
69
+ else:
70
+ attrs[col_name] = Column(
71
+ type=col.type,
72
+ alias=col.alias,
73
+ nullable=col.nullable,
74
+ description=col.description,
75
+ )
76
+
77
+ for cs_name, cs in schema_a.column_sets().items():
78
+ attrs[cs_name] = ColumnSet(
79
+ members=cs.members.copy() if isinstance(cs.members, list) else cs.members,
80
+ type=cs.type,
81
+ regex=cs.regex,
82
+ description=cs.description,
83
+ )
84
+
85
+ for cs_name, cs in schema_b.column_sets().items():
86
+ if cs_name not in attrs:
87
+ attrs[cs_name] = ColumnSet(
88
+ members=cs.members.copy() if isinstance(cs.members, list) else cs.members,
89
+ type=cs.type,
90
+ regex=cs.regex,
91
+ description=cs.description,
92
+ )
93
+
94
+ # Create the new schema class
95
+ combined_schema = type(combined_name, (BaseSchema,), attrs)
96
+
97
+ # Set names on Column/ColumnSet descriptors
98
+ for attr_name, attr_value in attrs.items():
99
+ attr_value.__set_name__(combined_schema, attr_name)
100
+
101
+ return combined_schema