diff options
author | Marc Mueller <30130371+cdce8p@users.noreply.github.com> | 2021-05-31 14:42:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-31 14:42:04 +0200 |
commit | 1ddc3ff7b3633ddd364a9c1a98e8e2b7dc4e9f21 (patch) | |
tree | 0fc3be0936315e1cea0d0d58a404036a2dfe1e67 /tests/functional/c | |
parent | 8115d8e810a8abdb8c3830a459787afa1ee37ca0 (diff) | |
download | pylint-git-1ddc3ff7b3633ddd364a9c1a98e8e2b7dc4e9f21.tar.gz |
Add new checker consider-using-namedtuple (#4517)
* Add new checker consider-using-namedtuple
* Add disable for existing code base
Diffstat (limited to 'tests/functional/c')
-rw-r--r-- | tests/functional/c/consider/consider_using_namedtuple.py | 74 | ||||
-rw-r--r-- | tests/functional/c/consider/consider_using_namedtuple.txt | 5 |
2 files changed, 79 insertions, 0 deletions
diff --git a/tests/functional/c/consider/consider_using_namedtuple.py b/tests/functional/c/consider/consider_using_namedtuple.py new file mode 100644 index 000000000..1bc150430 --- /dev/null +++ b/tests/functional/c/consider/consider_using_namedtuple.py @@ -0,0 +1,74 @@ +# pylint: disable=missing-docstring,too-few-public-methods,unused-variable,no-name-in-module +from typing import Final + +class Foo: + BAR = "bar" + +KEY_3 = "key_3" + + +# Subdicts have at least 1 common key +MAPPING_1 = { # [consider-using-namedtuple] + "entry_1": {"key_1": 0, "key_2": 1, "key_diff_1": 2}, + "entry_2": {"key_1": 0, "key_2": 1, "key_diff_2": 3}, +} +MAPPING_2 = { # [consider-using-namedtuple] + "entry_1": {KEY_3: None, Foo.BAR: None}, + "entry_2": {KEY_3: None, Foo.BAR: None}, +} + +# ints are not valid fieldnames for namedtuples +MAPPING_3 = { + "entry_1": {0: None, 1: None}, + "entry_2": {0: None, 1: None}, +} + + +def func(): + # Not in module scope + mapping_4 = { + "entry_1": {"key_1": 0, "key_2": 1}, + "entry_2": {"key_1": 0, "key_2": 1}, + } + + mapping_5: Final = { # [consider-using-namedtuple] + "entry_1": {"key_1": 0, "key_2": 1}, + "entry_2": {"key_1": 0, "key_2": 1}, + } + + +# lists must have the same length +MAPPING_6 = { # [consider-using-namedtuple] + "entry_1": [1, "a", set()], + "entry_2": [2, "b", set()], +} +MAPPING_7 = { + "entry_1": [], + "entry_2": [], +} +MAPPING_8 = { + "entry_1": [1], + "entry_2": [2, "b"], +} +MAPPING_9 = { # [consider-using-namedtuple] + "entry_1": (1, "a"), + "entry_2": (2, "b"), +} + +# No entry can't contain only dicts +MAPPING_10 = { + "entry_1": [ + {"key_1": None, "key_2": None}, + ], + "entry_2": [None] +} + +# No either dict, tuple, or list as dict values +MAPPING_11 = { + "entry_1": 1, + "entry_2": 2, +} +MAPPING_12 = { + "entry_1": "", + "entry_2": "", +} diff --git a/tests/functional/c/consider/consider_using_namedtuple.txt b/tests/functional/c/consider/consider_using_namedtuple.txt new file mode 100644 index 000000000..3abb72f5f --- /dev/null +++ b/tests/functional/c/consider/consider_using_namedtuple.txt @@ -0,0 +1,5 @@ +consider-using-namedtuple:11:12::Consider using namedtuple for dictionary values +consider-using-namedtuple:15:12::Consider using namedtuple for dictionary values +consider-using-namedtuple:34:23:func:Consider using namedtuple for dictionary values +consider-using-namedtuple:41:12::Consider using namedtuple for dictionary values +consider-using-namedtuple:53:12::Consider using namedtuple for dictionary values |