The singleton is an accepted pattern in Python, and there are sometimes
good reasons for it. (Even if that reason is not obvious in the current
example.)
If we're going the metaclass route, why not just use enums?
class Nothing:
NOTHING = 'nothing'
def f(arg: Iterable[int] | Nothing = Nothing.NOTHING) -> int: ...
On Mon, Sep 27, 2021 at 6:17 PM Eric Traut
Brett, is there a reason you don't want to use `None` as a sentinel? It's the one singleton object whose "singleton-ness" is enforced by the runtime. Plus, all type checkers already have intimate knowledge of it, so `is not None` works for type narrowing.
Here's another idea to consider. Would it make sense to expose a new metaclass from stdlib (let's call it `SingletonMeta`) that enforces the singleton-ness of a class? Any class based on this metaclass could be instantiated only once, and an attempt to instantiate it a second time would result in a runtime exception. Type checkers could then assume that all instances of that class are the same object, and the `is X` and `is not X` type narrowing logic could take advantage of that knowledge.
-Eric
-- Eric Traut Contributor to pyright & pylance Microsoft Corp. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...