Skip to content

Native Guid <> UUID#800

Open
buvinghausen wants to merge 4 commits intotonybaloney:mainfrom
buvinghausen:add_guid_uuid
Open

Native Guid <> UUID#800
buvinghausen wants to merge 4 commits intotonybaloney:mainfrom
buvinghausen:add_guid_uuid

Conversation

@buvinghausen
Copy link
Copy Markdown
Contributor

@buvinghausen buvinghausen commented Mar 9, 2026

Add native Guid ↔ Python uuid.UUID support

Adds first-class support for System.Guid as a native interop type, mapping to and from Python's uuid.UUID. No manual string conversion or PyObject unwrapping is needed.

Usage

Python:

import uuid

def create_token() -> uuid.UUID:
    return uuid.uuid4()

def echo_id(value: uuid.UUID) -> uuid.UUID:
    return value

C#:

Guid token = module.CreateToken();
Guid same  = module.EchoId(Guid.NewGuid());

How it works

The conversion goes through Python's standard uuid module (always available in the stdlib):

  • C# → Python: PyObject.From(Guid) calls uuid.UUID(guid.ToString()) via the C API
  • Python → C#: PyObjectImporters.Uuid calls str(uuid_obj) and parses the result with Guid.Parse

Both directions use the canonical xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx string representation, which both Guid.ToString() and str(uuid.UUID(...)) produce by default.


Changes

Source generation (CSnakes.SourceGeneration)

File Change
Parser/Types/PythonTypeSpec.cs Added UuidType record and PythonTypeSpec.Uuid static instance
Parser/PythonParser.TypeDef.cs Parser now recognises UUID and uuid.UUID as UuidType
Reflection/TypeReflection.cs UuidType maps to Guid in both ToPython and FromPython directions
ResultConversionCodeGenerator.cs Added UuidType case generating .BareImportAs<Guid, PyObjectImporters.Uuid>()

Generated signature for a uuid.UUID return type:

Guid TestUuidRoundtrip(Guid value);

Runtime (CSnakes.Runtime)

File Change
PyObjectTypeConverter.Uuid.cs (new) ConvertFromGuid / ConvertToGuid conversion logic
Python/PyObjectImporters.cs Added Uuid importer
Python/PyObject.cs Added From(Guid), From(Guid?) overloads; Guid case in From(object?) and As<T>
PublicAPI/net8.0/PublicAPI.Unshipped.txt Declared new public API surface
PublicAPI/net9.0/PublicAPI.Unshipped.txt Declared new public API surface

Tests

File What it covers
CSnakes.Runtime.Tests/Converter/GuidConverterTest.cs (new) Round-trip via PyObject.From / As<Guid> and the Uuid importer
CSnakes.Tests/PythonTypeDefinitionParserTests.cs Parser accepts both UUID and uuid.UUID
CSnakes.Tests/TypeReflectionTests.cs Both spellings resolve to Guid
CSnakes.Tests/PythonStaticGeneratorTests/FormatClassFromMethods.test_uuid.approved.txt (new) Source generator snapshot
Integration.Tests/python/test_uuid.py (new) Python test module
Integration.Tests/UuidTests.cs (new) End-to-end integration tests

Copilot AI review requested due to automatic review settings March 9, 2026 02:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class interop support for mapping Python uuid.UUID values to/from .NET System.Guid, so generated bindings and runtime conversions no longer require manual string conversion or PyObject handling.

Changes:

  • Extend the source generator type system and parser to recognize UUID / uuid.UUID and reflect it as Guid.
  • Add runtime conversion + importer support (PyObject.From(Guid) / As<Guid> and PyObjectImporters.Uuid).
  • Add unit + integration tests and generator snapshot coverage for UUID/Guid round-tripping.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Integration.Tests/python/test_uuid.py Adds Python functions returning/accepting uuid.UUID for end-to-end integration coverage.
src/Integration.Tests/UuidTests.cs Adds integration tests validating Guid ↔ UUID conversion and parsing from string.
src/CSnakes.Tests/TypeReflectionTests.cs Verifies UUID and uuid.UUID resolve to Guid in reflection mapping.
src/CSnakes.Tests/PythonTypeDefinitionParserTests.cs Verifies the parser accepts both UUID and uuid.UUID.
src/CSnakes.Tests/PythonStaticGeneratorTests/FormatClassFromMethods.test_uuid.approved.txt Snapshot showing generated signatures and return conversion using the UUID importer.
src/CSnakes.SourceGeneration/ResultConversionCodeGenerator.cs Adds UUID result conversion generator case targeting Guid + PyObjectImporters.Uuid.
src/CSnakes.SourceGeneration/Reflection/TypeReflection.cs Maps UuidType to Guid in both conversion directions.
src/CSnakes.SourceGeneration/Parser/Types/PythonTypeSpec.cs Introduces UuidType and PythonTypeSpec.Uuid.
src/CSnakes.SourceGeneration/Parser/PythonParser.TypeDef.cs Recognizes UUID / uuid.UUID as UuidType.
src/CSnakes.Runtime/Python/PyObjectImporters.cs Adds PyObjectImporters.Uuid importer for Guid.
src/CSnakes.Runtime/Python/PyObject.cs Adds PyObject.From(Guid/Guid?), Guid case in From(object?), and As<Guid> support.
src/CSnakes.Runtime/PyObjectTypeConverter.Uuid.cs Implements the actual Guiduuid.UUID conversion logic.
src/CSnakes.Runtime/PublicAPI/net8.0/PublicAPI.Unshipped.txt Declares newly added public API members for net8.0.
src/CSnakes.Runtime/PublicAPI/net9.0/PublicAPI.Unshipped.txt Declares newly added public API members for net9.0.
src/CSnakes.Runtime.Tests/Converter/GuidConverterTest.cs Adds runtime converter tests for multiple Guid cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@buvinghausen
Copy link
Copy Markdown
Contributor Author

@atifaziz or @tonybaloney y'all still around?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants