If you want to do type-checking, I don't think this a good way to do it. As a sibling comment points out, it doesn't work all that well since many objects respond to `len()`. I think it would be preferable to add type annotations and use mypy to check them or perhaps, in rare circumstances, do an isinstance check.
Alas, I find this suggestion unpersuasive for the use I have in mind. I get that there would be cases it does apply to.
I just want to enforce that the object acts like a list, mainly versus a float, int, or boolean. Tuples are definitely 100% ok, and if someone wants to give me a set, that's almost certainly OK too, in the use cases I'm considering. And so is a numpy vector!
Often these objects are held in a container (a dict or class variable), so type annotations would be clumsy as far as I know. Wouldn't I need to create a separate object to enforce the annotation?
And the type annotation would need to allow tuples and all flavors of lists or numpy arrays.
On the other end of the scale, I don't think either of us wants to use isinstance checks!
My primary intent isn't to be bulletproof, it's to show intent. The main fail of using len() in this way is that it accepts strings.
I don't see how using len() this way is any different than doing an isinstance check. You're essentially saying, "If this object doesn't respond to len(), raise a TypeError."
Perhaps true but misses the point. If you’re using len() for type checking, it’s essentially no different than doing isinstance(items, Sized). The latter explicitly calls out what you’re doing and makes it clear, to me, that’s it’s not really correct, in that it doesn’t show your intent, which is to enforce a sequence of items in a container not that the object has a size.
“supports len()” and “is a Sequence” are fairly reliably equivalent, and using len() fails fast in many cases (e.g., iteration over a potentially infinite iterable) not doing so would not and be less desirable.
Type checking via mypy, pyright, etc., is great, but in library (as opposed to an app) you can't count on consumers doing that, and isinstance() checks don't support duck-typing.