Skip to content

Commit

Permalink
Tease apart py_function and py-overloaded responsibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jmanuel1 committed Nov 10, 2024
1 parent d47fcf4 commit a2b4bdc
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 121 deletions.
27 changes: 23 additions & 4 deletions concat/tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
no_return_type,
optional_type,
py_function_type,
py_overloaded_type,
)
from hypothesis.strategies import ( # type: ignore
SearchStrategy,
Expand Down Expand Up @@ -57,6 +58,20 @@ def _object_type_strategy(
)


def _py_function_strategy(
individual_type_strategy: SearchStrategy[IndividualType],
) -> SearchStrategy[PythonFunctionType]:
return builds(
lambda args: py_function_type[args],
tuples(
_type_sequence_strategy(
individual_type_strategy, no_rest_var=True
),
individual_type_strategy,
),
)


_individual_type_subclasses = IndividualType.__subclasses__()
_individual_type_strategies = {}

Expand Down Expand Up @@ -86,14 +101,18 @@ def _mark_individual_type_strategy(
| _mark_individual_type_strategy(
_object_type_strategy(children), ObjectType
)
| _mark_individual_type_strategy(
_py_function_strategy(children),
PythonFunctionType,
)
| _mark_individual_type_strategy(
builds(
lambda args: py_function_type[args],
tuples(
_type_sequence_strategy(children, no_rest_var=True), children
lambda arg: py_overloaded_type[arg],
_type_sequence_strategy(
_py_function_strategy(children), no_rest_var=True
),
),
PythonFunctionType,
type(py_overloaded_type[()]),
)
| _mark_individual_type_strategy(
builds(
Expand Down
4 changes: 2 additions & 2 deletions concat/typecheck/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ def infer(
f'Decorators produce too many stack items: only 1 should be left. Stack: {final_type_stack.output}'
)
final_type = final_type_stack_output[0]
if not isinstance(final_type, IndividualType):
if not (final_type.kind <= ItemKind):
raise TypeError(
f'Decorators should produce something of individual type, got {final_type}'
f'Decorators should produce something of item kind, got {final_type}'
)
gamma |= {
name: (
Expand Down
18 changes: 18 additions & 0 deletions concat/typecheck/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,21 @@ def format_substitution_kind_error(variable: Variable, ty: Type) -> str:
f'{variable} is being substituted by {ty}, which has the wrong kind '
f'({variable.kind} vs {ty.kind})'
)


def format_not_generic_type_error(ty: Type) -> str:
return f'{ty} is not a generic type (has kind {ty.kind})'


def format_not_allowed_as_overload_error(ty: Type) -> str:
return f'{ty} cannot be the type of an overload of a Python function'


def format_sequence_var_must_be_only_arg_of_py_overloaded(
var: Variable,
) -> str:
return f'{var} must be the only argument of py_overloaded'


def format_rigid_variable_error(var: Variable, ty: Type) -> str:
return f'{var} is rigid and cannot be unified with {ty}'
Loading

0 comments on commit a2b4bdc

Please sign in to comment.