Reference implementation of sentinels for the Python stdlib

Overview
Comments
  • Question on sentinel instantiation

    Question on sentinel instantiation

    Hi,

    Just wonder what'd happen to a is sentinel checks if a user is messing with relative/absolute imports or sentinels are spawned in different threads.

    I've have had a good time years ago while debugging a similar case of sentinel = object() and is checks, which lead me to sentinel = tuple() approach.

    opened by idlesign 5
  • minor edits plus some coments

    minor edits plus some coments

    In addition:

    • In Rationale, criteria 2 and 6 can probably be merged.

    Is it assumed that the sentinel function is always called at module scope? I think there may be thread safety issues with the sentinel() function.

    opened by iritkatriel 5
  • Add to builtins

    Add to builtins

    Hey Tal,

    Instead of creating a module with a single function, and since sentinel is used 468 times in 58 different python files just in the standard library, I believe it justifies adding it to the builtins. image

    The usage of sentinel values is vast, and almost every Python module I write or contribute to has created at least one sentinel value.

    Importing the module just about everywhere sounds a bit counter-productive, as creating sentinel objects should be as simple as creating a string, a "None" object, or an integer. Plus, I do not see how the "sentinels" module grows to have more than the single "sentinel" class. No matter how it grows, it'll break the uniqueness and thus one of the requirements of a sentinel value.

    Lots of love ❤️ Bar Harel.

    opened by bharel 3
  • Disable extendability

    Disable extendability

    Thank you for a nice and clean idea and a reference implementation to provide a standard way to define a sentinel.

    I'd like to ask to improve reference implementation with closing ability to define any properties or extending the object.

    With reference implementation I see usage of slots mechanism and making this class final. There's no reason to provide extendable implementation for sentinels as they are as a None just singletons.

    opened by eirnym 2
  • instead of hacking the class into globals, ensure that reduce/picling can restore the object

    instead of hacking the class into globals, ensure that reduce/picling can restore the object

    if the sentinel type was simply available on the sentinel, that would be good enough for a number of cases

    MyMissing = sentinel("missing", module=__name__)
    assert isinstance(MyMissing, MyMissing.type
    

    pickle could aslo be set up to just return them as the value stored in the module

    opened by RonnyPfannschmidt 1
  • Specific type signatures for each sentinel value

    Specific type signatures for each sentinel value

    I really like the proposed Sentinel class because when using an Enum for a singleton type it always annoyingly shows up in type signatures and autogenerated documentation.

    However I think not supporting Literal as mentioned in Specific type signatures for each sentinel value is a big mistake, since it would mean Sentinel could not actually replace Enum if you require the ability to use the type in Literal ... which I think is a very common requirement.

    Python notably does not support algebraic data types. For example in Rust you could just define:

    enum Submodule {
        Specific(String),
        All,
        AllRecursively,
    }
    

    the closest you can do in Python is to use the Union type, for example Union[str, Literal[DirectSubmodules, AllSubmodules]], now instead of having to abuse Enum for this as follows:

    class _ModuleSpec(Enum):
        DirectSubmodules = 'DirectSubmodules'
        AllSubmodules = 'AllSubmodules'
    
    DirectSubmodules = _ModuleSpec.DirectSubmodules
    AllSubmodules = _ModuleSpec.AllSubmodules
    

    (which would result in _ModuleSpec showing up in IDE tooltips and autogenerated API documentation and thus potentially confuse users), it would be great if you could instead just do:

    DirectSubmodules = Sentinel('DirectSubmodules')
    AllSubmodules = Sentinel('AllSubmodules')
    

    and still be able to use Literal[DirectSubmodules, AllSubmodules].

    The PEP mentions that Literal support has been dropped from the PEP due to concerns voiced on the mailing list. While I cannot really comment on the challenges of special casing Literal, I think this additional complexity would definitely be worth it to make Sentinel as useful as Enum and allow it to replace it for cases where you do not actually want to introduce an enum type.

    opened by not-my-profile 2
  • May `Sentinel` be subclassed?

    May `Sentinel` be subclassed?

    The specification currently does not specify whether or not the Sentinel class may be subclassed. The reference implementation does not use @typing.final, allowing subclasses, which I think does make sense because it allows e.g. the __str__ or __le__ methods of sentinel objects to be customized, which can be very useful.

    I think it would make sense to clarify the specification to say that the Sentinel class may be subclassed.

    opened by not-my-profile 0
  • Singular or plural for the module name

    Singular or plural for the module name

    The Specification section of PEP 661 currently says:

    A new Sentinel class will be added to a new sentinels module.

    Note "sentinels" in plural but goes on with a code example showing sentinel in singular:

     >>> from sentinel import Sentinel
    

    While the latter appears to be a typo, I think singular would actually be preferable to plural to be consistent with the enum module, which is called enum instead of enums, so I think the sentinel module should also be called sentinel instead of sentinels.

    opened by not-my-profile 0
Owner
Tal Einat
Tal Einat
Optional static typing for Python 3 and 2 (PEP 484)

Mypy: Optional Static Typing for Python Got a question? Join us on Gitter! We don't have a mailing list; but we are always happy to answer questions o

Python 14.4k Jan 8, 2023
A Python Parser

parso - A Python Parser Parso is a Python parser that supports error recovery and round-trip parsing for different Python versions (in multiple Python

Dave Halter 520 Dec 26, 2022
A simple program which checks Python source files for errors

Pyflakes A simple program which checks Python source files for errors. Pyflakes analyzes programs and detects various errors. It works by parsing the

Python Code Quality Authority 1.2k Dec 30, 2022
Performant type-checking for python.

Pyre is a performant type checker for Python compliant with PEP 484. Pyre can analyze codebases with millions of lines of code incrementally – providi

Facebook 6.2k Jan 4, 2023
A static type analyzer for Python code

pytype - ?? ✔ Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can: Lint plain Python code, flagging c

Google 4k Dec 31, 2022
The strictest and most opinionated python linter ever!

wemake-python-styleguide Welcome to the strictest and most opinionated python linter ever. wemake-python-styleguide is actually a flake8 plugin with s

wemake.services 2.1k Jan 1, 2023
Static type checker for Python

Static type checker for Python Speed Pyright is a fast type checker meant for large Python source bases. It can run in a “watch” mode and performs fas

Microsoft 9.2k Jan 3, 2023
Tool to check the completeness of MANIFEST.in for Python packages

check-manifest Are you a Python developer? Have you uploaded packages to the Python Package Index? Have you accidentally uploaded broken packages with

Marius Gedminas 270 Dec 26, 2022
A python documentation linter which checks that the docstring description matches the definition.

Darglint A functional docstring linter which checks whether a docstring's description matches the actual function/method implementation. Darglint expe

Terrence Reilly 463 Dec 31, 2022
Flake8 plugin that checks import order against various Python Style Guides

flake8-import-order A flake8 and Pylama plugin that checks the ordering of your imports. It does not check anything else about the imports. Merely tha

Python Code Quality Authority 270 Nov 24, 2022
Flake8 extension for checking quotes in python

Flake8 Extension to lint for quotes. Major update in 2.0.0 We automatically encourage avoiding escaping quotes as per PEP 8. To disable this, use --no

Zachary Heller 157 Dec 13, 2022
Check for python builtins being used as variables or parameters

Flake8 Builtins plugin Check for python builtins being used as variables or parameters. Imagine some code like this: def max_values(list, list2):

Gil Forcada Codinachs 98 Jan 8, 2023
flake8 plugin to run black for checking Python coding style

flake8-black Introduction This is an MIT licensed flake8 plugin for validating Python code style with the command line code formatting tool black. It

Peter Cock 146 Dec 15, 2022
Custom Python linting through AST expressions

bellybutton bellybutton is a customizable, easy-to-configure linting engine for Python. What is this good for? Tools like pylint and flake8 provide, o

H. Chase Stevens 249 Dec 31, 2022
Unbearably fast O(1) runtime type-checking in pure Python.

Look for the bare necessities, the simple bare necessities. Forget about your worries and your strife. — The Jungle Book.

beartype 1.4k Jan 1, 2023
Naming Convention checker for Python

PEP 8 Naming Conventions Check your code against PEP 8 naming conventions. This module provides a plugin for flake8, the Python code checker. (It repl

Python Code Quality Authority 411 Dec 23, 2022
Code audit tool for python.

Pylama Code audit tool for Python and JavaScript. Pylama wraps these tools: pycodestyle (formerly pep8) © 2012-2013, Florent Xicluna; pydocstyle (form

Kirill Klenov 967 Jan 7, 2023
Flake8 extension for enforcing trailing commas in python

Flake8 Extension to enforce better comma placement. Usage If you are using flake8 it's as easy as: pip install flake8-commas Now you can avoid those a

Python Code Quality Authority 127 Sep 3, 2022
Tool for pinpointing circular imports in Python. Find cyclic imports in any project

Pycycle: Find and fix circular imports in python projects Pycycle is an experimental project that aims to help python developers fix their circular de

Vadim Kravcenko 311 Dec 15, 2022