Simple type aliases in pure-python mode?

16 views
Skip to first unread message

John Ehresman

unread,
May 12, 2025, 2:40:46 AMMay 12
to cython-users
I want to use simple type aliases in order to better document my code, use type checkers on it, and have cython recognize the base type of the object. For example, I’d like to write something like —

class Expr:
pass

ExprDict = dict[str,Expr]

def do_something(s: str, expr_dict: ExprDict) -> str:
pass

The code in the body of do_something() should use direct PyDict_* calls (or the equivalent) when for the expr_dict variable. Is this possible with cython 3.1?

Thanks,

John

David Woods

unread,
May 12, 2025, 3:48:42 AMMay 12
to cython...@googlegroups.com
Hi John,

No - Cython doesn't currently understand any kind of type alias.

You might be able to add `@cython.locals(expr_dict=dict)` to the function - i.e. keep the annotation for human readers but manually give the type for Cython. That's a bit verbose though.

(Note also that Cython won't currently make use of the key/value types in square brackets - you can specify them but they're ignored. But this is a bit tangential to what you're asking)


John Ehresman

unread,
May 12, 2025, 10:01:40 PMMay 12
to cython...@googlegroups.com
Thanks. I’m skeptical that using cython.locals and hints would work — I’ve been converting from the former to the latter and having both seems to cause problems (at least in 3.0). I also don’t want to duplicate info where I don’t need to.

Is there a guide to working with the cython compiler sources somewhere? I might take a look at trying to add some alias support.

John
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/cython-users/7FAE12A4-3455-4A0E-B950-AE5EE318084E%40d-woods.co.uk.

da-woods

unread,
May 13, 2025, 3:19:52 AMMay 13
to cython...@googlegroups.com

Hi John,

I’m skeptical that using cython.locals and hints would work
Yes - it's a suggestion but it probably wasn't the best one.
Is there a guide to working with the cython compiler sources somewhere? I might take a look at trying to add some alias support.

There's a little bit https://github.com/cython/cython/wiki/HackerGuide but it's pretty outdated and probably not too helpful to you.

Very rough outline about how you might do this: I can't promise it's all true though. After the "ControlFlowAnalysis" stage each entry with have a list of assignments (cf_assignments). (Although we might skip this for globals... I'm not completely sure... It'd be a problem here if we did). Essentially you'll be looking for an entry with a single assignment, where calling `analyse_as_type` gives you an answer. You probably want to do this in `NameNode.declare_from_annotation`.

A word of warning though: it's quite likely a big enough feature that we'd put it in Cython 3.2 so might not be usable hugely soon even if you got it working.

The other thing you could try is

ExprDict = cython.typedef(dict[str,Expr])

I'm not completely sure if that'll work, and it's a bit of a deviation from the pure "type annotation" syntax. But it's quick to test and find found.

David

Reply all
Reply to author
Forward
0 new messages