Skip to content

FA100 issue: Ruff doesnt suggest adding from __future__ import annotations #13273

@HappyCthulhu

Description

@HappyCthulhu

Search keywords

"TCH rules", "move under TYPE_CHECKING", "FA100"

Ruff Version

0.5.7

Config

Part of pyproject.toml
[tool.poetry.group.dev.dependencies]
ruff = "0.5.7"

[tool.ruff]
line-length = 120
extend-exclude = ["venv", "stubs"]

[tool.ruff.lint]
select = ["ALL"]
unfixable = ["ERA001", "COM819"] # remove comment out code lines
# COM819 prohibited-trailing-comma. Это фиксить должна format команда, чтоб как в black было
# если есть запятая в конце - раскрывает на несколько строк. Если нет - оставляет в одной
ignore = [
    "Q", # flake8-quotes
    "D100",	# undocumented-public-module	Missing docstring in public module
    "D101",	# undocumented-public-class	"Missing" # docstring in public class
    "D102",	# undocumented-public-method	Missing docstring "in" # public method
    "D103",	# undocumented-public-function	Missing docstring in "public" # function
    "D104",	# undocumented-public-package	Missing docstring in public package	"🛠"
    "D105",	# undocumented-magic-method	Missing docstring in magic method
    "D106",	# # undocumented-public-nested-class	Missing docstring in public nested class
    "D107",	# # undocumented-public-init	Missing docstring in __init__
    "ANN101", # missing-type-self
    "TD003", # missing-todo-link missing issue link on the line following this TODO
    "TD002", # missing-todo-author  missing author in TODO; try: `# TODO(<author_name>): ...` or `# TODO @<author_name>: ...`Ruff
    "S101", # assert Use of assert detected	
    "ANN002", #	missing-type-args	Missing type annotation for *{name}
    "ANN003", #	missing-type-kwargs	Missing type annotation for **{name}
    "RUF001", #	ambiguous-unicode-character-string	String contains ambiguous {}. Did you mean {}? Может на русскую "с" в комменте ругаться 
    "RUF003", #	ambiguous-unicode-character-comment
    "SLF001", # private-member-access	Private member accessed: {access} | учитывая, что в Django постоянно дергаем _Meta моделей и поля в духе _prefetched_objects_cache и т.д.
    # "FA100", # future-rewritable-type-annotation	Missing from __future__ import annotations, but uses {name}	
    # "FA102", # future-required-type-annotation	Missing from __future__ import annotations, but uses
    "DJ001", # django-nullable-model-string-field	Avoid using null=True on string-based fields such as	
    "PGH004", # Use specific rule codes when using `noqa`
    "TRY003", # Avoid specifying long messages outside the exception class
    "EM102", # Exception must not use an f-string literal, assign to variable first
    "RUF002", # Docstring contains ambiguous `у` (CYRILLIC SMALL LETTER U). Did you mean `y` (LATIN SMALL LETTER Y)?
    "FBT001", # Boolean-typed positional argument in function definition
    "FBT002", # Boolean default positional argument in function definition
    "ANN204", # Missing return type annotation for special method `__init__`
]

[tool.ruff.lint.flake8-annotations]
mypy-init-return = true # ANN204 | checkers often allow you to omit the return type annotation for __init__ methods, as long as at least one argument has a type annotation

[tool.ruff.lint.isort]
force-wrap-aliases = true
combine-as-imports = true

Bug Report

My request:

I would like all imports that are used only for type hints to be placed under a TYPE_CHECKING block. If you have not yet implemented this function, please consider this issue as a feature request.

Sometimes ruff doesn't detect imports, that are used only for type-hints without from __future__ import anotations. I know, that existing of from __future__ import anotations is one of requirements for ruff to make give TCH-warnings. But sometimes FA100 rules doesnt detect cases, when import is used only for type-hints. I will make an example for django-migration:

# Generated by Django 4.0.2 on 2022-09-19 12:51


from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps


def migrate(apps: StateApps, _schema: BaseDatabaseSchemaEditor) -> None:
    apps.get_model("app_name", "ModelName").objects.all().delete()


class Migration(migrations.Migration):
    dependencies = []  # noqa: RUF012

    operations = [  # noqa: RUF012
        migrations.RunPython(
            code=migrate,
            reverse_code=migrations.RunPython.noop,
        ),
    ]


image

No ruff-warnings are shown in this code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions