Skip to content

invalid-sentinel - why does the argument have to be a literal? #4142

Description

@DetachHead

https://pyrefly.org/en/docs/error-kinds/#invalid-sentinel

# First argument passed to sentinel constructor isn't a string literal
my_str: str = "MISSING"
A = Sentinel(my_str)

this requirement doesn't seem to be mentioned in the docs or the PEP, but interestingly pyright also reports the same error. (ty doesn't)

i'm wondering if theres some documentation somewhere i'm missing, or if this just came from an early draft of the PEP that was later removed?

Activity

  1. yangdanny97 commented on Jul 21, 2026

    @yangdanny97
    Collaborator

    I don't think it's a hard constraint, but we can't validate that the variable has the same name as the sentinel if we don't know the name statically

  2. added
    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.
    on Jul 21, 2026
  3. DetachHead commented on Jul 27, 2026

    @DetachHead
    ContributorAuthor

    ah, i must've missed this part of the PEP:

    If the name passed to sentinel() does not match the name the object is assigned to, type checkers should emit an error.

    ideally the type checker shouldn't have special-case an error when the argument isn't a LiteralString though. that's typeshed's job. see these PRs:

  4. DetachHead commented on Jul 27, 2026

    @DetachHead
    ContributorAuthor

    I don't think it's a hard constraint, but we can't validate that the variable has the same name as the sentinel if we don't know the name statically

    looks like pyrefly currently doesn't do that:

    foo = sentinel("bar") # no error

    sandbox

  5. AlexWaygood commented on Jul 27, 2026

    @AlexWaygood

    I don't think it's a hard constraint, but we can't validate that the variable has the same name as the sentinel if we don't know the name statically

    Note that python/typing#2277 proposes to remove this text when adding this PEP to the spec and conformance suite.

  6. self-assigned this
    on Jul 29, 2026
  7. rchen152 commented on Jul 29, 2026

    @rchen152
    Contributor

    I took a quick look, and pyrefly already falls back to the assigned name when sentinel gets a non-literal argument, so I think we can relax this restriction. The discussion on the conformance test is ongoing, but in the current state, we don't need to check that the names match. We can add this error back if that changes.

  8. added a commit that references this issue on Jul 30, 2026
  9. added a commit that references this issue on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.typechecking

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions