{"id":9981,"library":"niltype","title":"niltype: Singleton for Missing Values","description":"A small Python library that provides a singleton `Nil` object. This object is designed to represent missing or unset values in scenarios where `None` is a valid and meaningful data value, thus avoiding ambiguity. Currently at version 1.0.2, it's a stable, single-purpose library with infrequent updates.","status":"active","version":"1.0.2","language":"en","source_language":"en","source_url":"https://github.com/tsv1/niltype","tags":["singleton","missing values","null object","data validation","sentinel"],"install":[{"cmd":"pip install niltype","lang":"bash","label":"Install niltype"}],"dependencies":[],"imports":[{"symbol":"Nil","correct":"from niltype import Nil"}],"quickstart":{"code":"from niltype import Nil\n\ndef get_user_setting(user_id: int, setting_name: str):\n    \"\"\"Simulates fetching a user setting where Nil means 'not found'.\"\"\"\n    if user_id == 1:\n        if setting_name == \"theme\":\n            return \"dark\"\n        elif setting_name == \"timeout\":\n            return None # None is a valid timeout (e.g., no timeout)\n    return Nil # Setting does not exist for this user\n\n# Example usage:\nsetting = get_user_setting(1, \"theme\")\nprint(f\"Theme setting: {setting} (is Nil: {setting is Nil})\")\n\nsetting = get_user_setting(1, \"timeout\")\nprint(f\"Timeout setting: {setting} (is Nil: {setting is Nil})\")\n\nsetting = get_user_setting(2, \"language\")\nprint(f\"Language setting: {setting} (is Nil: {setting is Nil})\")\n\nif setting is Nil:\n    print(\"\\nSetting 'language' for user 2 is missing.\")\nelif setting is None:\n    print(\"\\nSetting 'timeout' is explicitly set to None (no timeout).\")\nelse:\n    print(f\"\\nSetting is {setting}.\")","lang":"python","description":"Demonstrates how to import and use the `Nil` singleton to distinguish between a truly missing value (represented by `Nil`) and an intentional `None` value in a function's return, especially useful for API responses or configuration."},"warnings":[{"fix":"Always use identity checks (`if obj is Nil:` or `if obj is not Nil:`) when specifically checking for the `Nil` singleton. Avoid `if not obj:` if you need to distinguish `Nil` from other falsey values.","message":"The `Nil` object evaluates to `False` in a boolean context, similar to `None`, empty collections, or zero. However, `Nil` is not `None` (`Nil != None`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"This is by design and fundamental to `niltype`. It implies `Nil` cannot carry state or attributes specific to a particular missing value context. If you need contextual information, wrap `Nil` in a custom class or use a different pattern.","message":"`Nil` is a true singleton, meaning there is only one instance of it throughout your application's lifetime. Any variable assigned `Nil` will reference this single instance.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Always check `if my_var is not Nil:` before attempting to access properties or methods on `my_var`, as `Nil` has no user-defined attributes.","cause":"Attempting to access attributes or methods on a variable that holds `Nil` without first checking for its presence.","error":"AttributeError: 'NilType' object has no attribute 'some_property'"},{"fix":"Implement explicit checks for `Nil` and handle it appropriately (e.g., return early, provide a default value, raise a more specific exception) before passing the variable to type-sensitive operations. For example: `my_str = 'Default' if my_var is Nil else str(my_var)`.","cause":"Passing `Nil` directly to functions or operations that expect a specific data type (e.g., string concatenation, arithmetic operations, database queries) and do not explicitly handle `Nil`.","error":"TypeError: can't concat NilType to str"}]}