{"id":13733442,"url":"https://github.com/MartinThoma/flake8-simplify","last_synced_at":"2025-05-08T09:32:04.985Z","repository":{"id":39222585,"uuid":"296913741","full_name":"MartinThoma/flake8-simplify","owner":"MartinThoma","description":"❄ A flake8 plugin that helps you to simplify code","archived":false,"fork":false,"pushed_at":"2023-12-25T09:43:40.000Z","size":221,"stargazers_count":192,"open_issues_count":56,"forks_count":21,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-12T22:38:10.939Z","etag":null,"topics":["code-quality","flake8","flake8-extensions","flake8-plugin","linter","python","python-style","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MartinThoma.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-09-19T16:59:41.000Z","updated_at":"2025-04-03T12:03:50.000Z","dependencies_parsed_at":"2023-12-25T10:45:14.637Z","dependency_job_id":"ca2183b4-35ef-4cd7-a386-dae0775fff3a","html_url":"https://github.com/MartinThoma/flake8-simplify","commit_stats":{"total_commits":131,"total_committers":12,"mean_commits":"10.916666666666666","dds":"0.10687022900763354","last_synced_commit":"8c43a67c65fd0b16a1b50fe3bf360b161fbb4b37"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinThoma%2Fflake8-simplify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinThoma%2Fflake8-simplify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinThoma%2Fflake8-simplify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MartinThoma%2Fflake8-simplify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MartinThoma","download_url":"https://codeload.github.com/MartinThoma/flake8-simplify/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253036594,"owners_count":21844242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-quality","flake8","flake8-extensions","flake8-plugin","linter","python","python-style","python3"],"created_at":"2024-08-03T03:00:43.113Z","updated_at":"2025-05-08T09:32:03.929Z","avatar_url":"https://github.com/MartinThoma.png","language":"Python","readme":"[![PyPI version](https://badge.fury.io/py/flake8-simplify.svg)](https://badge.fury.io/py/flake8-simplify)\n[![Code on Github](https://img.shields.io/badge/Code-GitHub-brightgreen)](https://github.com/MartinThoma/flake8-simplify)\n[![Actions Status](https://github.com/MartinThoma/flake8-simplify/workflows/Unit%20Tests/badge.svg)](https://github.com/MartinThoma/flake8-simplify/actions)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n# flake8-simplify\n\nA [flake8](https://flake8.pycqa.org/en/latest/index.html) plugin that helps you simplify your code.\n\n## Installation\n\nInstall with `pip`:\n\n```bash\npip install flake8-simplify\n```\n\nPython 3.8 to 3.11 are supported.\n\n\n## Usage\n\nJust call `flake8 .` in your package or `flake your.py`:\n\n```\n$ flake8 .\n./foo/__init__.py:690:12: SIM101 Multiple isinstance-calls which can be merged into a single call for variable 'other'\n```\n\n\n## Rules\n\nPython-specific rules:\n\n* `SIM101`: Multiple isinstance-calls which can be merged into a single call by\n  using a tuple as a second argument ([example](#SIM101))\n* [`SIM104`](https://github.com/MartinThoma/flake8-simplify/issues/4) ![](https://shields.io/badge/-legacyfix-inactive): Use 'yield from iterable' (introduced in Python 3.3, see [PEP 380](https://docs.python.org/3/whatsnew/3.3.html#pep-380)) ([example](#SIM104))\n* [`SIM105`](https://github.com/MartinThoma/flake8-simplify/issues/5): Use ['contextlib.suppress(...)'](https://docs.python.org/3/library/contextlib.html#contextlib.suppress) instead of try-except-pass ([example](#SIM105))\n* [`SIM107`](https://github.com/MartinThoma/flake8-simplify/issues/9): Don't use `return` in try/except and finally  ([example](#SIM107))\n* [`SIM108`](https://github.com/MartinThoma/flake8-simplify/issues/12): Use the ternary operator if it's reasonable  ([example](#SIM108))\n* [`SIM109`](https://github.com/MartinThoma/flake8-simplify/issues/11): Use a tuple to compare a value against multiple values ([example](#SIM109))\n* [`SIM110`](https://github.com/MartinThoma/flake8-simplify/issues/15): Use [any(...)](https://docs.python.org/3/library/functions.html#any)  ([example](#SIM110))\n* [`SIM111`](https://github.com/MartinThoma/flake8-simplify/issues/15): Use [all(...)](https://docs.python.org/3/library/functions.html#all) ([example](#SIM111))\n* [`SIM113`](https://github.com/MartinThoma/flake8-simplify/issues/18): Use enumerate instead of manually incrementing a counter ([example](#SIM113))\n* [`SIM114`](https://github.com/MartinThoma/flake8-simplify/issues/10): Combine conditions via a logical or to prevent duplicating code ([example](#SIM114))\n* [`SIM115`](https://github.com/MartinThoma/flake8-simplify/issues/17): Use context handler for opening files ([example](#SIM115))\n* [`SIM116`](https://github.com/MartinThoma/flake8-simplify/issues/31): Use a dictionary instead of many if/else equality checks ([example](#SIM116))\n* [`SIM117`](https://github.com/MartinThoma/flake8-simplify/issues/35): Merge with-statements that use the same scope ([example](#SIM117))\n* `SIM119`: ![](https://img.shields.io/badge/-removed-lightgrey) Moved to [flake8-scream](https://github.com/MartinThoma/flake8-scream) due to [issue 63](https://github.com/MartinThoma/flake8-simplify/issues/63)\n* `SIM120` ![](https://shields.io/badge/-legacyfix-inactive): Use 'class FooBar:' instead of 'class FooBar(object):' ([example](#SIM120))\n* `SIM121`: Reserved for [SIM908](#sim908) once it's stable\n* `SIM125`: Reserved for [SIM905](#sim905) once it's stable\n* `SIM126`: Reserved for [SIM906](#sim906) once it's stable\n* `SIM127`: Reserved for [SIM907](#sim907) once it's stable\n\nSimplifying Comparisons:\n\n* `SIM201`: Use 'a != b' instead of 'not a == b' ([example](#SIM201))\n* `SIM202`: Use 'a == b' instead of 'not a != b' ([example](#SIM202))\n* `SIM203`: Use 'a not in b' instead of 'not (a in b)' ([example](#SIM203))\n* `SIM204`: ![](https://img.shields.io/badge/-removed-lightgrey) Moved to [flake8-scream](https://github.com/MartinThoma/flake8-scream) due to [issue 116](https://github.com/MartinThoma/flake8-simplify/issues/116)\n* `SIM205`: ![](https://img.shields.io/badge/-removed-lightgrey) Moved to [flake8-scream](https://github.com/MartinThoma/flake8-scream) due to [issue 116](https://github.com/MartinThoma/flake8-simplify/issues/116)\n* `SIM206`: ![](https://img.shields.io/badge/-removed-lightgrey) Moved to [flake8-scream](https://github.com/MartinThoma/flake8-scream) due to [issue 116](https://github.com/MartinThoma/flake8-simplify/issues/116)\n* `SIM207`: ![](https://img.shields.io/badge/-removed-lightgrey) Moved to [flake8-scream](https://github.com/MartinThoma/flake8-scream) due to [issue 116](https://github.com/MartinThoma/flake8-simplify/issues/116)\n* `SIM208`: Use 'a' instead of 'not (not a)' ([example](#SIM208))\n* `SIM210`: Use 'bool(a)' instead of 'True if a else False' ([example](#SIM210))\n* `SIM211`: Use 'not a' instead of 'False if a else True' ([example](#SIM211))\n* [`SIM212`](https://github.com/MartinThoma/flake8-simplify/issues/6): Use 'a if a else b' instead of 'b if not a else a' ([example](#SIM212))\n* [`SIM220`](https://github.com/MartinThoma/flake8-simplify/issues/6): Use 'False' instead of 'a and not a' ([example](#SIM220))\n* [`SIM221`](https://github.com/MartinThoma/flake8-simplify/issues/6): Use 'True' instead of 'a or not a' ([example](#SIM221))\n* [`SIM222`](https://github.com/MartinThoma/flake8-simplify/issues/6): Use 'True' instead of '... or True' ([example](#SIM222))\n* [`SIM223`](https://github.com/MartinThoma/flake8-simplify/issues/6): Use 'False' instead of '... and False' ([example](#SIM223))\n* [`SIM224`](https://github.com/MartinThoma/flake8-simplify/issues/88): Reserved for [SIM901](#sim901) once it's stable\n* [`SIM300`](https://github.com/MartinThoma/flake8-simplify/issues/16): Use 'age == 42' instead of '42 == age' ([example](#SIM300))\n\nSimplifying usage of dictionaries:\n\n* [`SIM401`](https://github.com/MartinThoma/flake8-simplify/issues/72): Use 'a_dict.get(key, \"default_value\")' instead of an if-block ([example](#SIM401))\n* [`SIM118`](https://github.com/MartinThoma/flake8-simplify/issues/40): Use 'key in dict' instead of 'key in dict.keys()' ([example](#SIM118))\n* `SIM119` Reserved for [SIM911](#sim911) once it's stable\n\nGeneral Code Style:\n\n* `SIM102`: Use a single if-statement instead of nested if-statements ([example](#SIM102))\n* [`SIM103`](https://github.com/MartinThoma/flake8-simplify/issues/3): Return the boolean condition directly ([example](#SIM103))\n* [`SIM106`](https://github.com/MartinThoma/flake8-simplify/issues/8): Handle error-cases first ([example](#SIM106)). This rule was removed due to too many false-positives.\n* [`SIM112`](https://github.com/MartinThoma/flake8-simplify/issues/19): Use CAPITAL environment variables ([example](#SIM112))\n* `SIM122` / SIM902: ![](https://img.shields.io/badge/-removed-lightgrey) Moved to [flake8-scream](https://github.com/MartinThoma/flake8-scream) due to [issue 125](https://github.com/MartinThoma/flake8-simplify/issues/125)\n* `SIM123` / SIM902: ![](https://img.shields.io/badge/-removed-lightgrey) Moved to [flake8-scream](https://github.com/MartinThoma/flake8-scream) due to [issue 130](https://github.com/MartinThoma/flake8-simplify/issues/130)\n* `SIM124`: Reserved for SIM909 once it's stable\n\n**Experimental rules:**\n\nGetting rules right for every possible case is hard. I don't want to disturb\npeoples workflows. For this reason, flake8-simplify introduces new rules with\nthe `SIM9` prefix. Every new rule will start with SIM9 and stay there for at\nleast 6 months. In this time people can give feedback. Once the rule is stable,\nthe code will change to another number.\n\nCurrent experimental rules:\n\n* `SIM901`: Use comparisons directly instead of wrapping them in a `bool(...)` call ([example](#SIM901))\n* `SIM904`: Assign values to dictionary directly at initialization ([example](#SIM904))\n* [`SIM905`](https://github.com/MartinThoma/flake8-simplify/issues/86): Split string directly if only constants are used ([example](#SIM905))\n* [`SIM906`](https://github.com/MartinThoma/flake8-simplify/issues/101): Merge nested os.path.join calls ([example](#SIM906))\n* [`SIM907`](https://github.com/MartinThoma/flake8-simplify/issues/64): Use Optional[Type] instead of Union[Type, None] ([example](#SIM907))\n* [`SIM908`](https://github.com/MartinThoma/flake8-simplify/issues/50): Use dict.get(key) ([example](#SIM908))\n* [`SIM909`](https://github.com/MartinThoma/flake8-simplify/issues/114): Avoid reflexive assignments ([example](#SIM909))\n* [`SIM910`](https://github.com/MartinThoma/flake8-simplify/issues/171): Avoid to use `dict.get(key, None)` ([example](#SIM910))\n* [`SIM911`](https://github.com/MartinThoma/flake8-simplify/issues/161): Avoid using `zip(dict.keys(), dict.values())` ([example](#SIM911))\n\n## Disabling Rules\n\nYou might have good reasons to\n[ignore some flake8 rules](https://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html).\nTo do that, use the standard Flake8 configuration. For example, within the `setup.cfg` file:\n\n```python\n[flake8]\nignore = SIM106, SIM113, SIM119, SIM9\n```\n\n\n## Examples\n\n### SIM101\n\n```python\n# Bad\nisinstance(a, int) or isinstance(a, float)\n\n# Good\nisinstance(a, (int, float))\n```\n\n### SIM102\n\n```python\n# Bad\nif a:\n    if b:\n        c\n\n# Good\nif a and b:\n    c\n```\n\n### SIM105\n\n```python\n# Bad\ntry:\n    foo()\nexcept ValueError:\n    pass\n\n# Good\nfrom contextlib import suppress\n\nwith suppress(ValueError):\n    foo()\n```\n\nPlease note that `contextlib.suppress` is roughly 3x slower than `try/except`\n([source](https://github.com/MartinThoma/flake8-simplify/issues/91)).\n\n### SIM107\n\n```python\n# Bad: This example returns 3!\ndef foo():\n    try:\n        1 / 0\n        return \"1\"\n    except:\n        return \"2\"\n    finally:\n        return \"3\"\n\n\n# Good\ndef foo():\n    return_value = None\n    try:\n        1 / 0\n        return_value = \"1\"\n    except:\n        return_value = \"2\"\n    finally:\n        return_value = \"3\"  # you might also want to check if \"except\" happened\n    return return_value\n```\n\n### SIM108\n\n```python\n# Bad\nif a:\n    b = c\nelse:\n    b = d\n\n# Good\nb = c if a else d\n```\n\n### SIM109\n\n```python\n# Bad\nif a == b or a == c:\n    d\n\n# Good\nif a in (b, c):\n    d\n```\n\n### SIM110\n\n```python\n# Bad\nfor x in iterable:\n    if check(x):\n        return True\nreturn False\n\n# Good\nreturn any(check(x) for x in iterable)\n```\n\n### SIM111\n\n```python\n# Bad\nfor x in iterable:\n    if check(x):\n        return False\nreturn True\n\n# Good\nreturn all(not check(x) for x in iterable)\n```\n\n### SIM112\n\n```python\n# Bad\nos.environ[\"foo\"]\nos.environ.get(\"bar\")\n\n# Good\nos.environ[\"FOO\"]\nos.environ.get(\"BAR\")\n```\n\n### SIM113\n\nUsing [`enumerate`](https://docs.python.org/3/library/functions.html#enumerate) in simple cases like the loops below is a tiny bit easier to read as the reader does not have to figure out where / when the loop variable is incremented (or if it is incremented in multiple places).\n\n```python\n# Bad\nidx = 0\nfor el in iterable:\n    ...\n    idx += 1\n\n# Good\nfor idx, el in enumerate(iterable):\n    ...\n```\n\n### SIM114\n\n```python\n# Bad\nif a:\n    b\nelif c:\n    b\n\n# Good\nif a or c:\n    b\n```\n\n### SIM115\n\n```python\n# Bad\nf = open(...)\n...  # (do something with f)\nf.close()\n\n# Good\nwith open(...) as f:\n    ...  # (do something with f)\n```\n\n### SIM116\n\n```python\n# Bad\nif a == \"foo\":\n    return \"bar\"\nelif a == \"bar\":\n    return \"baz\"\nelif a == \"boo\":\n    return \"ooh\"\nelse:\n    return 42\n\n# Good\nmapping = {\"foo\": \"bar\", \"bar\": \"baz\", \"boo\": \"ooh\"}\nreturn mapping.get(a, 42)\n```\n\n### SIM117\n\n```python\n# Bad\nwith A() as a:\n    with B() as b:\n        print(\"hello\")\n\n# Good\nwith A() as a, B() as b:\n    print(\"hello\")\n```\n\nThank you for pointing this one out, [Aaron Gokaslan](https://github.com/Skylion007)!\n\n### SIM118\n\n```python\n# Bad\nkey in a_dict.keys()\n\n# Good\nkey in a_dict\n```\n\nThank you for pointing this one out, [Aaron Gokaslan](https://github.com/Skylion007)!\n\n\n### SIM120\n\n```python\n# Bad\nclass FooBar(object):\n    ...\n\n\n# Good\nclass FooBar:\n    ...\n```\n\nBoth notations are equivalent in Python 3, but the second one is shorter.\n\n\n### SIM201\n\n```python\n# Bad\nnot a == b\n\n# Good\na != b\n```\n\n### SIM202\n\n```python\n# Bad\nnot a != b\n\n# Good\na == b\n```\n\n### SIM203\n\n```python\n# Bad\nnot a in b\n\n# Good\na not in b\n```\n\n\n### SIM208\n\n```python\n# Bad\nnot (not a)\n\n# Good\na\n```\n\n### SIM210\n\n```python\n# Bad\nTrue if a else False\n\n# Good\nbool(a)\n```\n\n### SIM211\n\n```python\n# Bad\nFalse if a else True\n\n# Good\nnot a\n```\n\n### SIM212\n\n```python\n# Bad\nb if not a else a\n\n# Good\na if a else b\n```\n\n### SIM220\n\n```python\n# Bad\na and not a\n\n# Good\nFalse\n```\n\n### SIM221\n\n```python\n# Bad\na or not a\n\n# Good\nTrue\n```\n\n### SIM222\n\n```python\n# Bad\n... or True\n\n# Good\nTrue\n```\n\n### SIM223\n\n```python\n# Bad\n... and False\n\n# Good\nFalse\n```\n\n### SIM300\n\n```python\n# Bad; This is called a \"Yoda-Condition\"\n42 == age\n\n# Good\nage == 42\n```\n\n### SIM401\n\n```python\n# Bad\nif key in a_dict:\n    value = a_dict[key]\nelse:\n    value = \"default_value\"\n\n# Good\nthing = a_dict.get(key, \"default_value\")\n```\n\n### SIM901\n\nThis rule will be renamed to `SIM224` after its 6-month trial period is over.\nPlease report any issues you encounter with this rule!\n\nThe trial period starts on 23rd of January and will end on 23rd of August 2022.\n\n```python\n# Bad\nbool(a == b)\n\n# Good\na == b\n```\n\n\n### SIM904\n\nThis rule will be renamed to `SIM224` after its 6-month trial period is over.\nPlease report any issues you encounter with this rule!\n\nThe trial period starts on 12th of February and will end on 12th of September 2022.\n\n\n```python\n# Bad\na = {}\na[\"b\"] = \"c\"\n\n# Good\na = {\"b\": \"c\"}\n```\n\n### SIM905\n\nThis rule will be renamed to `SIM225` after its 6-month trial period is over.\nPlease report any issues you encounter with this rule!\n\nThe trial period starts on 13th of February and will end on 13th of September 2022.\n\n```python\n# Bad\ndomains = \"de com net org\".split()\n\n# Good\ndomains = [\"de\", \"com\", \"net\", \"org\"]\n```\n\n### SIM906\n\nThis rule will be renamed to `SIM126` after its 6-month trial period is over.\nPlease report any issues you encounter with this rule!\n\nThe trial period starts on 20th of February and will end on 20th of September 2022.\n\n```python\n# Bad\nos.path.join(a, os.path.join(b, c))\n\n# Good\nos.path.join(a, b, c)\n```\n\n### SIM907\n\nThis rule will be renamed to `SIM127` after its 6-month trial period is over.\nPlease report any issues you encounter with this rule!\n\nThe trial period starts on 28th of March and will end on 28th of September 2022.\n\n```python\n# Bad\ndef foo(a: Union[int, None]) -\u003e Union[int, None]:\n    return a\n\n\n# Good\ndef foo(a: Optional[int]) -\u003e Optional[int]:\n    return a\n```\n\n### SIM908\n\nThis rule will be renamed to `SIM121` after its 6-month trial period is over.\nPlease report any issues you encounter with this rule!\n\nThe trial period starts on 28th of March and will end on 28th of September 2022.\n\n```python\n# Bad\nname = \"some_default\"\nif \"some_key\" in some_dict:\n    name = some_dict[\"some_key\"]\n\n# Good\nname = some_dict.get(\"some_key\", \"some_default\")\n```\n\n### SIM909\n\nThank you Ryan Delaney for the idea!\n\nThis rule will be renamed to `SIM124` after its 6-month trial period is over.\nPlease report any issues you encounter with this rule!\n\nThe trial period starts on 28th of March and will end on 28th of September 2022.\n\n```python\n# Bad\nfoo = foo\n\n# Good: Nothing. Reflexive assignments have no purpose.\n```\n\nor\n\n```python\n# Bad\nfoo = foo = 42\n\n# Good\nfoo = 42\n```\n\n### SIM910\n\n```python\n# Bad\ndict.get(key, None)\n\n# Good\ndict.get(key)\n```\n","funding_links":[],"categories":["Topics Index","Linters \u0026 Style Checkers","All-in-one"],"sub_categories":["Code Quality and Linting"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMartinThoma%2Fflake8-simplify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMartinThoma%2Fflake8-simplify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMartinThoma%2Fflake8-simplify/lists"}