Skip to content

Commit

Permalink
Merge pull request #19 from routablehq/DEV-15802-linting-rule-for-cre…
Browse files Browse the repository at this point in the history
…ating-feature-flags-in-code

[DEV-15802] Linting rule for creating FeatureFlags in code
  • Loading branch information
justmobilize authored Mar 7, 2024
2 parents 1b84f42 + 4af397f commit fcafcd4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: "3.8"
python-version: "3.11"
- name: Install dependencies
run: |
pip install -r requirements-dev.txt
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repos:
rev: 23.3.0
hooks:
- id: black
language_version: python3.8
language_version: python3.11


- repo: https://github.com/pycqa/flake8
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Here is a list of the rules supported by this Flake8 plugin:
* `ROU105` - Constants are not in order
* `ROU106` - Relative imports are not allowed
* `ROU107` - Inline function import is not at top of statement
* `ROU108` - Import from model module instead of sub-packages
* `ROU109` - Disallow rename migrations
* `ROU111` - Disallow FeatureFlag creation in code

## Testing

Expand Down
31 changes: 31 additions & 0 deletions flake8_routable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Python imports
import ast
import importlib.metadata as importlib_metadata
import re
import tokenize
import warnings
from dataclasses import dataclass
Expand Down Expand Up @@ -33,6 +34,7 @@
ROU107 = "ROU107 Inline function import is not at top of statement"
ROU108 = "ROU108 Import from model module instead of sub-packages"
ROU109 = "ROU109 Disallow rename migrations"
ROU111 = "ROU111 Disallow FeatureFlag creation in code"


@dataclass
Expand Down Expand Up @@ -178,6 +180,7 @@ def visit(self, file_tokens: List[tokenize.TokenInfo]) -> None:
self.lines_with_invalid_docstrings()
self.lines_with_invalid_multi_line_strings()
self.rename_migrations()
self.disallow_feature_flag_creation()

def lines_with_blank_lines_after_comments(self) -> None:
"""
Expand Down Expand Up @@ -346,6 +349,34 @@ def rename_migrations(self) -> None:
reported.add(line_token.start[0])
self.errors.append((*line_token.start, ROU109))

def disallow_feature_flag_creation(self) -> None:
"""We can not create FeatureFlags in code, they are cached on the request."""
reported = set()
feature_flag_creation = re.compile(r".+(FeatureFlag\.objects\..*create)")
allowed_comments = [
"# valid for legacy cross-border work",
"# valid for management command",
"# valid for test usage",
]

for line_token in self._file_tokens:
if line_token.start[0] in reported:
# There could be many tokens on a same line.
continue

line = line_token.line

if not feature_flag_creation.match(line):
# Skip lines that don't match
continue

if any(comment in line for comment in allowed_comments):
# Ignore lines with these comments, as they are valid
continue

reported.add(line_token.start[0])
self.errors.append((*line_token.start, ROU111))


class Plugin:
"""Flake8 plugin for Routable's best coding practices."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.black]
line-length = 120
target-version = ['py38']
target-version = ['py311']

[tool.isort]
case_sensitive = true
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = flake8_routable
version = 0.1.2
version = 0.1.3

[options]
py_modules = flake8_routable
Expand Down

0 comments on commit fcafcd4

Please sign in to comment.