Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False negative TC100 #146

Open
sondrelg opened this issue Nov 26, 2022 · 1 comment
Open

False negative TC100 #146

sondrelg opened this issue Nov 26, 2022 · 1 comment

Comments

@sondrelg
Copy link
Member

This example in https://github.com/snok/drf-openapi-tester isn't flagged with a TC100 when it should be. The Response annotation will otherwise lead to a runtime error.

from typing import TYPE_CHECKING

from django.urls import reverse
from rest_framework.test import APITestCase

from openapi_tester import SchemaTester
from tests.utils import TEST_ROOT

if TYPE_CHECKING:
    from rest_framework.response import Response

schema_tester = SchemaTester(schema_file_path=str(TEST_ROOT) + "/schemas/sample-schemas/content_types.yaml")


class BaseAPITestCase(APITestCase):
    """Base test class for api views including schema validation"""

    @staticmethod
    def assertResponse(response: Response, **kwargs) -> None:
        """helper to run validate_response and pass kwargs to it"""
        schema_tester.validate_response(response=response, **kwargs)


class PetsAPITests(BaseAPITestCase):
    def test_get_pet_by_id(self):
        response = self.client.get(
            reverse(
                "get-pet",
                kwargs={
                    "petId": 1,
                },
            ),
            content_type="application/vnd.api+json",
        )
        assert response.status_code == 200
        self.assertResponse(response)
@sondrelg
Copy link
Member Author

sondrelg commented Aug 4, 2023

Looks like the error is actually caught by the plugin, but is not emitted.

❯ flake8 test.py -v 
flake8.checker            MainProcess    139 INFO     Making checkers
self.options.enable_extensions=None
flake8.type_checking      MainProcess    140 INFO     Optional warning TC100 Add 'from __future__ import annotations' import not present in selected warnings: ('TC', 'PL', 'C90', 'F', 'E', 'W'). Not running it at all.
self.options.enable_extensions=None
flake8.type_checking      MainProcess    140 INFO     Optional warning TC200 Annotation 'Response' needs to be made into a string literal not present in selected warnings: ('TC', 'PL', 'C90', 'F', 'E', 'W'). Not running it at all.
flake8.main.application   MainProcess    147 INFO     Finished running
flake8.main.application   MainProcess    147 INFO     Reporting errors
flake8.main.application   MainProcess    148 INFO     Found a total of 0 violations and reported 0

Running it with --select TC100 gives the expected result 🤔

❯ flake8 test.py --select TC100
test.py:1:1: TC100 Add 'from __future__ import annotations' import

Not sure what the issue is, but seems to have something to do with config loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant