Hacker News new | ask | show | jobs
by winter_blue 5011 days ago
Thanks for mentioning pytyp!

I have been using this recipe: http://code.activestate.com/recipes/572161/ to add type checking to Python for a while now, so it's nice to see a more mature and developed library that does the same (and more.)

In your code examples I didn't see any decorators around the functions that were being typechecked, so how do you then enforce the type checking?

In addition, is pytyp as fully-featured as the recipe above? (The recipe is short but well-written.) The comments at the beginning of the recipe shows a bunch of examples that demonstrate all that it can do.

1 comments

huh. i will fix that, thanks. there should be a @checked annotation on the function that is checked in the example there.

pyptyp is pretty comprehensive. for example, Rec(a=Seq(Opt(int)),b=Alt(float,str)) is the type for something like a dict where a is a list that contains ints or None and b is either a float or a string.

BUT it's pure python so it's not at all fast. i wouldn't use type checking throughout a codebase - only for debugging, or tests.

it's perhaps more useful as a start for building other things. for example, it includes mapping from JSON to python objects - it can use type annotations to guide the construction of python objects from JSON lists and maps. but on the other hand the internals are quite complex (i have been working with it recently, after not using it for a year or so, and it's taken some effort to understand everything).

Another similar project: https://github.com/podio/valideer. The (rough) equivalent schema for the example above can be expressed as {"a": ["?integer"], "b": AnyOf("number", "string")}.
also, i should perhaps mention, compared to some other libraries, pytyp is integrated with ABCs and the Python type system. so you can use isinstance() with these expressions, for example.