Live data from Hacker News

Counting Sheeps with Contracts in Python

colorsofcode.ghost.io

11–19 of 19 posts

Re: Counting Sheeps with Contracts in Python

#11
Python shouldn't need special lib to design by contract, `assert cond, "reason"` and `if __debug__` work very well for that.

Unfortunately, people don't know they are made to be stripped in prod with `-OO` so that the perf hit is zero. This means you can never be sure a 3rd party lib you depend on is not using an assert somewhere for a very important check you are going to disable.

https://www.bitecode.dev/p/the-best-python-feature-you-canno...

Re: Counting Sheeps with Contracts in Python

#13

One of the most 'nice' thing about contract systems is automating blame checking in higher-order code. They handle the lambda/method param wrapping and blame direction assignment for you, which is annoying to do via manual assertions. I didn't see a link to DBC, is that in there? Or some other use of contracts that is hard via just asserts, like in test amplification? We are always looking for ways to improve our Pyt…

It's at the end of the article in one of those "card" links.

https://gitlab.com/leogermond/python-dbc

Re: Counting Sheeps with Contracts in Python

#14

Python shouldn't need special lib to design by contract, `assert cond, "reason"` and `if __debug__` work very well for that. Unfortunately, people don't know they are made to be stripped in prod with `-OO` so that the perf hit is zero. This means you can never be sure a 3rd party lib you depend on is not using an assert somewhere for a very important check you are going to disable. https://www.bitecode.dev/p/the-best…

More from the official docs: https://docs.python.org/3/reference/simple_stmts.html#index-...

Re: Counting Sheeps with Contracts in Python

#15
Value types with contracts should be the focus here. Specifically, the story should not be of type str, but rather of type story that is only allowed to exist when it meets certain conditions.

This approach ensures only a single point of validation, a lower mental overhead as you are certain it is valid everywhere, and fast failure when it's not - by contract, even.

There are a few places where this concept is discussed, but the most concise phrase is: “Parse, don’t validate.”

Zod (TypeScript) is a good example of a library like this, as it is oriented around types. I’m not sure if Python has something similar.

However, overvalidation can make your code overly verbose, which is something I’ve fallen into before. Use it sparingly.

Re: Counting Sheeps with Contracts in Python

#16

One of the most 'nice' thing about contract systems is automating blame checking in higher-order code. They handle the lambda/method param wrapping and blame direction assignment for you, which is annoying to do via manual assertions. I didn't see a link to DBC, is that in there? Or some other use of contracts that is hard via just asserts, like in test amplification? We are always looking for ways to improve our Pyt…

It's at the end of the article in one of those "card" links. https://gitlab.com/leogermond/python-dbc

[deleted]

Re: Counting Sheeps with Contracts in Python

#17

One of the most 'nice' thing about contract systems is automating blame checking in higher-order code. They handle the lambda/method param wrapping and blame direction assignment for you, which is annoying to do via manual assertions. I didn't see a link to DBC, is that in there? Or some other use of contracts that is hard via just asserts, like in test amplification? We are always looking for ways to improve our Pyt…

It's at the end of the article in one of those "card" links. https://gitlab.com/leogermond/python-dbc

Ah yeah unfortunate, afaict it does not (yet?) support lambdas, which is where imo this starts supporting flows that are hard via manual asserts

I was hoping something like a more modern https://dl.acm.org/doi/10.1145/581478.581484

Re: Counting Sheeps with Contracts in Python

#18
post #3

This isn't meant as a dig, I'm genuinely curious: how is this not simply imposing static typing in python?

It kind of is, but pre/post-conditions can be more than that. Also, I'd consider it bad form to have type signatures (which they have) and runtime checking the same types. The whole point of having static typing is decreasing the need for the latter, but you have to actually run a type checker for it to be useful.
Post reply on HN