Skip to content

PYI002: Confusing/Unclear error messageΒ #27324

Closed

Description

@wangenau

Summary

Given a simple stub file, e.g., test.pyi with the following content

import numpy as np

x: object
if np.ma.is_mask(x): ...

will result in the following output when running ruff check test.pyi with the new default rules

PYI002 `if` test must be a simple comparison against `sys.platform` or `sys.version_info`
 --> test.pyi:4:4
  |
3 | x: object
4 | if np.ma.is_mask(x): ...
  |    ^^^^^^^^^^^^^^^^
  |

Found 1 error.

This does not look quite right πŸ˜„

Version

ruff 0.16.0

Activity

  1. ntBre commented on Jul 30, 2026

    @ntBre
    Contributor

    The diagnostic does seem a bit misleading, but I think the rule is acting as intended. Is this the kind of code that caused the diagnostic in a real project? The rule is just pointing out that type checkers only recognize certain kinds of simple conditionals in stub files, and this is not one of them.

  2. added
    questionAn issue asking for support or clarification
    on Jul 30, 2026
  3. wangenau commented on Jul 30, 2026

    @wangenau
    Author

    Yes, the code is adapted from https://github.com/numpy/numpy/blob/main/numpy/typing/tests/data/reveal/ma.pyi#L370.
    The rule is not activated in their repo, I was just playing around with it.
    And now I understand the reasoning of the message.
    The error message is indeed confusing, since no sys.platform or sys.version_info are in the actual code.
    But then it might be a false positive from my side πŸ˜„

  4. ntBre commented on Jul 30, 2026

    @ntBre
    Contributor

    I'm not a typing expert, but it seems pretty unusual to have a full function definition in a stub file since they aren't evaluated at runtime. If you end up activating PYI002 in numpy, this file would likely be a good use for per-file-ignores or another ignore mechanism.

    Regardless, I wouldn't be opposed to trying to improve the diagnostic. It feels like it's missing some kind of explanatory note about type checkers or at least mentioning stub files.

  5. added
    diagnosticsDiagnostic messages, annotations, fixes & rendering infra--not for PRs that just add new diagnostics
    and removed
    questionAn issue asking for support or clarification
    on Jul 30, 2026
  6. Avasam commented on Aug 12, 2026

    @Avasam
    Contributor

    AFAICT, if np.ma.is_mask(x): ... does nothing for any type checker, and I don't see why it would (outside of a really, really weird custom mypy plugin).

    The error message could mention "in stubs".

    A helper message could be added to direct the user to simply remove the statement, if no sys.platform or sys.version_info (or adjacent) is detected at all.


    unrecognized-version-info-check (PYI003) already handles validating that a version check is supported / in spec.

    The same "it's a version check, but an unsupported one" detection could be leveraged to not guide the user to remove the statement in PYI002 if but rather to simplify it.


    Or keep it simple with a conditional prose:

    help: If this is intended as a version or platform check, simplify it to something that's supported by type-checkers (see doc). Otherwise, remove this condition.

  7. jonathandung commented on Aug 13, 2026

    @jonathandung
    Contributor

    The title should be changed.

  8. changed the title [-]`PYI002`: False positive[/-] [+]`PYI002`: Confusing/Unclear error message[/+] on Aug 13, 2026
  9. added a commit that references this issue on Sep 17, 2026
    3709494
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsDiagnostic messages, annotations, fixes & rendering infra--not for PRs that just add new diagnostics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions