Add PythonTypeSpec subclasses for recognised Python types#669
Add PythonTypeSpec subclasses for recognised Python types#669tonybaloney merged 21 commits intotonybaloney:mainfrom
PythonTypeSpec subclasses for recognised Python types#669Conversation
5125c77 to
e0eb619
Compare
Conflicts resolved: - src/CSnakes.SourceGeneration/Reflection/ArgumentReflection.cs
| .Subscript() | ||
| select new PythonTypeSpec(n, [..callable.Parameters.Append(callable.Return)]), | ||
| "Literal" => | ||
| typeDefinitionParser.ManyDelimitedBy(comma) |
There was a problem hiding this comment.
I know this is already an issue, but this fails the parser:
P = ParamSpec("P")
T_co = TypeVar("T_co", covariant=True)
def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]:There was a problem hiding this comment.
Does this need to be resolved in this PR if it was already an issue?
I don't think a decorator function would be a target for the source generator. We already ignore functions starting with an underscore, but for such a case where a decorator is part of a module and name mangling isn't desired, I can imagine supporting something along the lines of # type: ignore comments that would act as a directive for the source generator. Like so?
P = ParamSpec("P")
T_co = TypeVar("T_co", covariant=True)
def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]: # csharp: ignoreThat said, one could support Callable where the first type argument is not a subscript and map it to ParsedPythonTypeSpec, but this would technically achieve something and generate a function on the C# end that would never be desired or used.
There was a problem hiding this comment.
It doesn't need to be fixed in this PR, but it's worth noting in the comment that there are circumstances when it's not another subscripted type (eg a type alias)
There was a problem hiding this comment.
Note added with 963029c.
I can imagine supporting something along the lines of
# type: ignorecomments that would act as a directive for the source generator.
Tracking in #684.
tonybaloney
left a comment
There was a problem hiding this comment.
This is a great improvement and removes a lot of the brittleness in the parser/source generator logic.
Approved in principle once you're finished
PythonTypeSpec subclasses for recognised Python typesPythonTypeSpec subclasses for recognised Python types
This PR represents a significant refactoring of the Python AST in CSnakes, transforming the loosely-typed, string-based
PythonTypeSpecclass into a comprehensive, strongly-typed hierarchy of record types. This change enhances type safety, improves performance through better caching, and provides a more maintainable foundation for Python-to-C# type mapping.Some of this work will help with adding type annotations support too, for help with PR #584.
Design Changes
Core Design Transformation
The new design introduces a hierarchy of immutable record types, each representing a recognised Python type:
Type System Enhancements
1. Singleton Pattern for Primitive Types
Built-in Python types like
int,str,bool, etc., are now implemented as singletons, reducing memory allocation and improving equality comparisons:2. Strongly-Typed Generic Types
Complex types now have strongly-typed properties instead of generic argument arrays:
Likewise, the
LiteralTypeonly accepts an array ofPythonConstant, which should help with #24 and fixes problems in tests likeLiteral[10]being seen asLiteral[Literal]:CSnakes/src/CSnakes.Tests/TokenizerTests.cs
Lines 110 to 112 in 4c5d3b6
3. Advanced Union Type Normalization
The
UnionType.Normalizemethod implements sophisticated logic to simplify union types:Union[int, None]→Optional[int]Union[int]→intThis functionality existed already but is now housed in the new
UnionType.4. ValueArray Collection Type
Introduced a new
ValueArray<T>record struct that provides:Technical Implementation Details
Parser Enhancements
The type definition parser has been refactored considerably to recognise the new type hierarchy early during parsing:
collections.abc.Sequencecollections.abc.Mappingcollections.abc.Generatorcollections.abc.Coroutinetyping.Uniontyping.LiteralCallableortyping.Callableorcollections.abc.Callabletuple[int, ...], includingtupleto meantuple[Any, ...]Code Generation Updates
All code generation components have been updated to work with the new type system:
Performance Improvements
Testing Coverage
The PR includes comprehensive test coverage with 399 new test cases covering:
Benefits
Type Safety
Maintainability
typing.OptionalandOptionalare in one place (the parser) instead of being spread outExtensibility
ISequenceType,IMappingType)