Skip to content

Conversation

@a-jean-andreasian
Copy link

@a-jean-andreasian a-jean-andreasian commented Nov 22, 2025

Feature or enhancement Issue #141838

Proposal:

Proposal: Add Empty Collection Type Hints for Enhanced Type Precision

Problem: The current type system doesn't distinguish between empty and non-empty containers, which can lead to ambiguity in function contracts and require additional code inspection.

Current limitation:

def get_user_data() -> List[Dict[str, str]]:
    # The return type doesn't indicate whether empty results are possible
    if not users_exist:
        return []  # Empty list
    return [{"name": "john"}]

Proposal: We suggest adding special forms for empty collections:

  • EmptyList for []
  • EmptyDict for {}
  • EmptySet for set()
  • EmptyTuple for ()

Use cases:

from typing import EmptyList, EmptyDict

# Clearer function contracts
def initialize() -> EmptyDict:
    return {}

# Optional results with precise types
def find_users(query: str) -> List[User] | EmptyList:
    if not query:
        return []
    return [user1, user2]

# Configuration with empty defaults
def load_settings() -> Dict[str, Setting] | EmptyDict:
    if not config_exists():
        return {}
    return parse_config()

Benefits:

  • More precise type annotations
  • Better communication of function semantics
  • Enhanced static analysis capabilities
  • Self-documenting code without runtime cost

Implementation consideration: These could be implemented as _SpecialForm instances, similar to existing special types, ensuring no runtime impact.

This would provide developers with additional expressiveness when designing APIs and function interfaces, particularly for cases where empty collections have specific semantic meaning.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

@python-cla-bot
Copy link

python-cla-bot bot commented Nov 22, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Nov 22, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@a-jean-andreasian a-jean-andreasian changed the title Add EmptyList, EmptyDict, EmptySet types for empty collection hinting Add EmptyList, EmptyDict, EmptySet types for empty collection hinting #141838 Nov 22, 2025
@picnixz
Copy link
Member

picnixz commented Nov 22, 2025

Closing because this needs a discussion. See #141838 (comment).

@picnixz picnixz closed this Nov 22, 2025
@a-jean-andreasian
Copy link
Author

Closing because this needs a discussion. See #141838 (comment).

The changes are minimal (+40 -0 lines) and straightforward to review. The implementation follows existing patterns of the module and adds clear value for type hinting empty collections.

The code is runnable and tested. If there are specific technical concerns, I'm happy to address them.

However, requiring extensive community discussion for a small, well-scoped addition seems disproportionate when the implementation work is already complete and follows established patterns.

Kindly regard, to not implement this idea in the future.

@terryjreedy terryjreedy changed the title Add EmptyList, EmptyDict, EmptySet types for empty collection hinting #141838 gh-141838: Add EmptyList, EmptyDict, EmptySet types for empty collection hinting Nov 22, 2025
@terryjreedy
Copy link
Member

Technical comment: PR titles, at least for CPython, are bi-directionally linked to issues as per my edit. The initial post should be much terser (often a single line) and not a repeat of the issue opening, as by default it becomes the commit message. With proper links, the repeat is redundant.

@JelleZijlstra
Copy link
Member

The runtime code may be small but objects in typing also need to be supported by type checkers, and it's not all that obvious how these would work in type checkers. If you want this to be part of Python, you'll have to write a detailed specification of how type checkers should handle these and ideally a reference implementation for at least one existing type checker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants