zon icon indicating copy to clipboard operation
zon copied to clipboard

feature request: `record`: return with strong typing

Open cachho opened this issue 10 months ago • 2 comments

Is your feature request related to a problem? Please describe. In zod, when I validate an object or record, the returned value of the parse operation is strongly typed, with all the keys and types specified in the schema.

Describe the solution you'd like I'd like record (and list) types to annotate the type that's the result of the validation, based on the schema.

At the moment it just shows the input type, so e.g. zon.element_list(zon.string()).validate([0]) should show list[str], but it actually shows list[int] right now. Obiously, the validation will either fail or return a list of strings.

Describe alternatives you've considered I'm not sure if this is possible in python like that.

Additional context I'm talking about this specifically in the context of type hints in vscode. I'm kind of new to statically typed python, so not sure if this has to do with the extensions I installed.

cachho avatar Feb 05 '25 09:02 cachho

Unfortunatelly, this is not possible to do in Python as it is in Typescript since both typesystems are a bit different. I know I looked into this when I first started this project and the conclusion was that I couldn't to that specific type inference for the example that you provided: there is no way for Python to infer the type of the inner validator like what is done in Typescript.

I looked this up a while ago and might no remember everything exactly, plust the language might have changed in the meantime. Will look into it when I do get the chance.

Naapperas avatar Feb 05 '25 16:02 Naapperas

Could we narrow down -- when talking about types are we talking about mypy or pyright or pyrefly (etc!)? Even in javascript there are two layers -- there's jsdoc and typescript - just happens they've got unified direction and support all through typescript as a plugin.

Downchuck avatar Aug 19 '25 16:08 Downchuck