astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
33.39k stars 1.11k forks source link

F722 and F821 errors reported using typing annotations. #2342

Closed KelSolaar closed 1 year ago

KelSolaar commented 1 year ago

Hello,

Using ruff 0.0.237, I get similar F722 and F821 errors as those reported in #548. I'm opening a new ticket as the previous one is old and closed.

Given the following:

from __future__ import annotations

import numpy as np

from colour.adaptation import CHROMATIC_ADAPTATION_TRANSFORMS
from colour.algebra import matrix_dot, vector_dot, sdiv, sdiv_mode
from colour.hints import ArrayLike, Literal, NDArrayFloat, Union
from colour.utilities import (
    from_range_1,
    row_as_diagonal,
    to_domain_1,
    validate_method,
)

def matrix_chromatic_adaptation_VonKries(
    XYZ_w: ArrayLike,
    XYZ_wr: ArrayLike,
    transform: Union[
        Literal[
            "Bianco 2010",
            "Bianco PC 2010",
            "Bradford",
            "CAT02 Brill 2008",
            "CAT02",
            "CAT16",
            "CMCCAT2000",
            "CMCCAT97",
            "Fairchild",
            "Sharp",
            "Von Kries",
            "XYZ Scaling",
        ],
        str,
    ] = "CAT02",
) -> NDArrayFloat:
    ...
colour/adaptation/vonkries.py:49:13: F722 Syntax error in forward annotation: `Bianco 2010`
colour/adaptation/vonkries.py:50:13: F722 Syntax error in forward annotation: `Bianco PC 2010`
colour/adaptation/vonkries.py:51:13: F821 Undefined name `Bradford`
colour/adaptation/vonkries.py:52:13: F722 Syntax error in forward annotation: `CAT02 Brill 2008`
colour/adaptation/vonkries.py:53:13: F821 Undefined name `CAT02`
colour/adaptation/vonkries.py:54:13: F821 Undefined name `CAT16`
colour/adaptation/vonkries.py:55:13: F821 Undefined name `CMCCAT2000`
colour/adaptation/vonkries.py:56:13: F821 Undefined name `CMCCAT97`
colour/adaptation/vonkries.py:57:13: F821 Undefined name `Fairchild`
colour/adaptation/vonkries.py:58:13: F821 Undefined name `Sharp`
colour/adaptation/vonkries.py:59:13: F722 Syntax error in forward annotation: `Von Kries`
colour/adaptation/vonkries.py:60:13: F722 Syntax error in forward annotation: `XYZ Scaling`

I only have 1413 of them! ;)

Cheers,

Thomas

charliermarsh commented 1 year ago

So, I think what you need to do here is tell Ruff that the imports from colour.hints should be treated as aliases for the typing module, like so:

[tool.ruff]
typing-modules = ["colour.hints"]

Can you give that a try? Will reopen if you're still seeing this of course!

KelSolaar commented 1 year ago

That was it, thank you!

crypdick commented 9 months ago

For future visitors, the solution above will throw a warning warning: The top-level linter settings are deprecated in favour of their counterparts in the lint section. Please update the following options in pyproject.toml: - 'typing-modules' -> 'lint.typing-modules' . The updated code is:

[tool.ruff.lint]
typing-modules = ["colour.hints"]