@@ -39,17 +39,26 @@ def _cfg_has_config(path: Path | str) -> bool:
39
39
return any (section .startswith ("pylint." ) for section in parser .sections ())
40
40
41
41
42
- def find_default_config_files () -> Iterator [Path ]:
43
- """Find all possible config files ."""
42
+ def _yield_default_files () -> Iterator [Path ]:
43
+ """Iterate over the default config file names and see if they exist ."""
44
44
for config_name in CONFIG_NAMES :
45
- if config_name .is_file ():
46
- if config_name .suffix == ".toml" and not _toml_has_config (config_name ):
47
- continue
48
- if config_name .suffix == ".cfg" and not _cfg_has_config (config_name ):
49
- continue
45
+ try :
46
+ if config_name .is_file ():
47
+ if config_name .suffix == ".toml" and not _toml_has_config (config_name ):
48
+ continue
49
+ if config_name .suffix == ".cfg" and not _cfg_has_config (config_name ):
50
+ continue
51
+
52
+ yield config_name .resolve ()
53
+ except OSError :
54
+ pass
55
+
50
56
51
- yield config_name .resolve ()
57
+ def _find_project_config () -> Iterator [Path ]:
58
+ """Traverse up the directory tree to find a config file.
52
59
60
+ Stop if no '__init__' is found and thus we are no longer in a package.
61
+ """
53
62
if Path ("__init__.py" ).is_file ():
54
63
curdir = Path (os .getcwd ()).resolve ()
55
64
while (curdir / "__init__.py" ).is_file ():
@@ -59,6 +68,9 @@ def find_default_config_files() -> Iterator[Path]:
59
68
if rc_path .is_file ():
60
69
yield rc_path .resolve ()
61
70
71
+
72
+ def _find_config_in_home_or_environment () -> Iterator [Path ]:
73
+ """Find a config file in the specified environment var or the home directory."""
62
74
if "PYLINTRC" in os .environ and Path (os .environ ["PYLINTRC" ]).exists ():
63
75
if Path (os .environ ["PYLINTRC" ]).is_file ():
64
76
yield Path (os .environ ["PYLINTRC" ]).resolve ()
@@ -68,16 +80,36 @@ def find_default_config_files() -> Iterator[Path]:
68
80
except RuntimeError :
69
81
# If the home directory does not exist a RuntimeError will be raised
70
82
user_home = None
83
+
71
84
if user_home is not None and str (user_home ) not in ("~" , "/root" ):
72
85
home_rc = user_home / ".pylintrc"
73
86
if home_rc .is_file ():
74
87
yield home_rc .resolve ()
88
+
75
89
home_rc = user_home / ".config" / "pylintrc"
76
90
if home_rc .is_file ():
77
91
yield home_rc .resolve ()
78
92
79
- if os .path .isfile ("/etc/pylintrc" ):
80
- yield Path ("/etc/pylintrc" ).resolve ()
93
+
94
+ def find_default_config_files () -> Iterator [Path ]:
95
+ """Find all possible config files."""
96
+ yield from _yield_default_files ()
97
+
98
+ try :
99
+ yield from _find_project_config ()
100
+ except OSError :
101
+ pass
102
+
103
+ try :
104
+ yield from _find_config_in_home_or_environment ()
105
+ except OSError :
106
+ pass
107
+
108
+ try :
109
+ if os .path .isfile ("/etc/pylintrc" ):
110
+ yield Path ("/etc/pylintrc" ).resolve ()
111
+ except OSError :
112
+ pass
81
113
82
114
83
115
def find_pylintrc () -> str | None :
0 commit comments