stdlib.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """Checkers for various standard library functions."""
  5. from __future__ import annotations
  6. import sys
  7. from collections.abc import Iterable
  8. from typing import TYPE_CHECKING, Any, Dict, Set, Tuple
  9. import astroid
  10. from astroid import nodes, util
  11. from astroid.typing import InferenceResult
  12. from pylint import interfaces
  13. from pylint.checkers import BaseChecker, DeprecatedMixin, utils
  14. from pylint.interfaces import HIGH, INFERENCE
  15. from pylint.typing import MessageDefinitionTuple
  16. if TYPE_CHECKING:
  17. from pylint.lint import PyLinter
  18. DeprecationDict = Dict[Tuple[int, int, int], Set[str]]
  19. OPEN_FILES_MODE = ("open", "file")
  20. OPEN_FILES_FUNCS = OPEN_FILES_MODE + ("read_text", "write_text")
  21. UNITTEST_CASE = "unittest.case"
  22. THREADING_THREAD = "threading.Thread"
  23. COPY_COPY = "copy.copy"
  24. OS_ENVIRON = "os._Environ"
  25. ENV_GETTERS = ("os.getenv",)
  26. SUBPROCESS_POPEN = "subprocess.Popen"
  27. SUBPROCESS_RUN = "subprocess.run"
  28. OPEN_MODULE = {"_io", "pathlib"}
  29. DEBUG_BREAKPOINTS = ("builtins.breakpoint", "sys.breakpointhook", "pdb.set_trace")
  30. LRU_CACHE = {
  31. "functools.lru_cache", # Inferred for @lru_cache
  32. "functools._lru_cache_wrapper.wrapper", # Inferred for @lru_cache() on >= Python 3.8
  33. "functools.lru_cache.decorating_function", # Inferred for @lru_cache() on <= Python 3.7
  34. }
  35. NON_INSTANCE_METHODS = {"builtins.staticmethod", "builtins.classmethod"}
  36. # For modules, see ImportsChecker
  37. DEPRECATED_ARGUMENTS: dict[
  38. tuple[int, int, int], dict[str, tuple[tuple[int | None, str], ...]]
  39. ] = {
  40. (0, 0, 0): {
  41. "int": ((None, "x"),),
  42. "bool": ((None, "x"),),
  43. "float": ((None, "x"),),
  44. },
  45. (3, 8, 0): {
  46. "asyncio.tasks.sleep": ((None, "loop"),),
  47. "asyncio.tasks.gather": ((None, "loop"),),
  48. "asyncio.tasks.shield": ((None, "loop"),),
  49. "asyncio.tasks.wait_for": ((None, "loop"),),
  50. "asyncio.tasks.wait": ((None, "loop"),),
  51. "asyncio.tasks.as_completed": ((None, "loop"),),
  52. "asyncio.subprocess.create_subprocess_exec": ((None, "loop"),),
  53. "asyncio.subprocess.create_subprocess_shell": ((4, "loop"),),
  54. "gettext.translation": ((5, "codeset"),),
  55. "gettext.install": ((2, "codeset"),),
  56. "functools.partialmethod": ((None, "func"),),
  57. "weakref.finalize": ((None, "func"), (None, "obj")),
  58. "profile.Profile.runcall": ((None, "func"),),
  59. "cProfile.Profile.runcall": ((None, "func"),),
  60. "bdb.Bdb.runcall": ((None, "func"),),
  61. "trace.Trace.runfunc": ((None, "func"),),
  62. "curses.wrapper": ((None, "func"),),
  63. "unittest.case.TestCase.addCleanup": ((None, "function"),),
  64. "concurrent.futures.thread.ThreadPoolExecutor.submit": ((None, "fn"),),
  65. "concurrent.futures.process.ProcessPoolExecutor.submit": ((None, "fn"),),
  66. "contextlib._BaseExitStack.callback": ((None, "callback"),),
  67. "contextlib.AsyncExitStack.push_async_callback": ((None, "callback"),),
  68. "multiprocessing.managers.Server.create": ((None, "c"), (None, "typeid")),
  69. "multiprocessing.managers.SharedMemoryServer.create": (
  70. (None, "c"),
  71. (None, "typeid"),
  72. ),
  73. },
  74. (3, 9, 0): {"random.Random.shuffle": ((1, "random"),)},
  75. }
  76. DEPRECATED_DECORATORS: DeprecationDict = {
  77. (3, 8, 0): {"asyncio.coroutine"},
  78. (3, 3, 0): {
  79. "abc.abstractclassmethod",
  80. "abc.abstractstaticmethod",
  81. "abc.abstractproperty",
  82. },
  83. (3, 4, 0): {"importlib.util.module_for_loader"},
  84. }
  85. DEPRECATED_METHODS: dict[int, DeprecationDict] = {
  86. 0: {
  87. (0, 0, 0): {
  88. "cgi.parse_qs",
  89. "cgi.parse_qsl",
  90. "ctypes.c_buffer",
  91. "distutils.command.register.register.check_metadata",
  92. "distutils.command.sdist.sdist.check_metadata",
  93. "tkinter.Misc.tk_menuBar",
  94. "tkinter.Menu.tk_bindForTraversal",
  95. }
  96. },
  97. 2: {
  98. (2, 6, 0): {
  99. "commands.getstatus",
  100. "os.popen2",
  101. "os.popen3",
  102. "os.popen4",
  103. "macostools.touched",
  104. },
  105. (2, 7, 0): {
  106. "unittest.case.TestCase.assertEquals",
  107. "unittest.case.TestCase.assertNotEquals",
  108. "unittest.case.TestCase.assertAlmostEquals",
  109. "unittest.case.TestCase.assertNotAlmostEquals",
  110. "unittest.case.TestCase.assert_",
  111. "xml.etree.ElementTree.Element.getchildren",
  112. "xml.etree.ElementTree.Element.getiterator",
  113. "xml.etree.ElementTree.XMLParser.getiterator",
  114. "xml.etree.ElementTree.XMLParser.doctype",
  115. },
  116. },
  117. 3: {
  118. (3, 0, 0): {
  119. "inspect.getargspec",
  120. "failUnlessEqual",
  121. "assertEquals",
  122. "failIfEqual",
  123. "assertNotEquals",
  124. "failUnlessAlmostEqual",
  125. "assertAlmostEquals",
  126. "failIfAlmostEqual",
  127. "assertNotAlmostEquals",
  128. "failUnless",
  129. "assert_",
  130. "failUnlessRaises",
  131. "failIf",
  132. "assertRaisesRegexp",
  133. "assertRegexpMatches",
  134. "assertNotRegexpMatches",
  135. },
  136. (3, 1, 0): {
  137. "base64.encodestring",
  138. "base64.decodestring",
  139. "ntpath.splitunc",
  140. "os.path.splitunc",
  141. "os.stat_float_times",
  142. "turtle.RawTurtle.settiltangle",
  143. },
  144. (3, 2, 0): {
  145. "cgi.escape",
  146. "configparser.RawConfigParser.readfp",
  147. "xml.etree.ElementTree.Element.getchildren",
  148. "xml.etree.ElementTree.Element.getiterator",
  149. "xml.etree.ElementTree.XMLParser.getiterator",
  150. "xml.etree.ElementTree.XMLParser.doctype",
  151. },
  152. (3, 3, 0): {
  153. "inspect.getmoduleinfo",
  154. "logging.warn",
  155. "logging.Logger.warn",
  156. "logging.LoggerAdapter.warn",
  157. "nntplib._NNTPBase.xpath",
  158. "platform.popen",
  159. "sqlite3.OptimizedUnicode",
  160. "time.clock",
  161. },
  162. (3, 4, 0): {
  163. "importlib.find_loader",
  164. "importlib.abc.Loader.load_module",
  165. "importlib.abc.Loader.module_repr",
  166. "importlib.abc.PathEntryFinder.find_loader",
  167. "importlib.abc.PathEntryFinder.find_module",
  168. "plistlib.readPlist",
  169. "plistlib.writePlist",
  170. "plistlib.readPlistFromBytes",
  171. "plistlib.writePlistToBytes",
  172. },
  173. (3, 4, 4): {"asyncio.tasks.async"},
  174. (3, 5, 0): {
  175. "fractions.gcd",
  176. "inspect.formatargspec",
  177. "inspect.getcallargs",
  178. "platform.linux_distribution",
  179. "platform.dist",
  180. },
  181. (3, 6, 0): {
  182. "importlib._bootstrap_external.FileLoader.load_module",
  183. "_ssl.RAND_pseudo_bytes",
  184. },
  185. (3, 7, 0): {
  186. "sys.set_coroutine_wrapper",
  187. "sys.get_coroutine_wrapper",
  188. "aifc.openfp",
  189. "threading.Thread.isAlive",
  190. "asyncio.Task.current_task",
  191. "asyncio.Task.all_task",
  192. "locale.format",
  193. "ssl.wrap_socket",
  194. "ssl.match_hostname",
  195. "sunau.openfp",
  196. "wave.openfp",
  197. },
  198. (3, 8, 0): {
  199. "gettext.lgettext",
  200. "gettext.ldgettext",
  201. "gettext.lngettext",
  202. "gettext.ldngettext",
  203. "gettext.bind_textdomain_codeset",
  204. "gettext.NullTranslations.output_charset",
  205. "gettext.NullTranslations.set_output_charset",
  206. "threading.Thread.isAlive",
  207. },
  208. (3, 9, 0): {
  209. "binascii.b2a_hqx",
  210. "binascii.a2b_hqx",
  211. "binascii.rlecode_hqx",
  212. "binascii.rledecode_hqx",
  213. },
  214. (3, 10, 0): {
  215. "_sqlite3.enable_shared_cache",
  216. "importlib.abc.Finder.find_module",
  217. "pathlib.Path.link_to",
  218. "zipimport.zipimporter.load_module",
  219. "zipimport.zipimporter.find_module",
  220. "zipimport.zipimporter.find_loader",
  221. "threading.currentThread",
  222. "threading.activeCount",
  223. "threading.Condition.notifyAll",
  224. "threading.Event.isSet",
  225. "threading.Thread.setName",
  226. "threading.Thread.getName",
  227. "threading.Thread.isDaemon",
  228. "threading.Thread.setDaemon",
  229. "cgi.log",
  230. },
  231. (3, 11, 0): {
  232. "locale.getdefaultlocale",
  233. "locale.resetlocale",
  234. "re.template",
  235. "unittest.findTestCases",
  236. "unittest.makeSuite",
  237. "unittest.getTestCaseNames",
  238. "unittest.TestLoader.loadTestsFromModule",
  239. "unittest.TestLoader.loadTestsFromTestCase",
  240. "unittest.TestLoader.getTestCaseNames",
  241. },
  242. },
  243. }
  244. DEPRECATED_CLASSES: dict[tuple[int, int, int], dict[str, set[str]]] = {
  245. (3, 2, 0): {
  246. "configparser": {
  247. "LegacyInterpolation",
  248. "SafeConfigParser",
  249. },
  250. },
  251. (3, 3, 0): {
  252. "importlib.abc": {
  253. "Finder",
  254. },
  255. "pkgutil": {
  256. "ImpImporter",
  257. "ImpLoader",
  258. },
  259. "collections": {
  260. "Awaitable",
  261. "Coroutine",
  262. "AsyncIterable",
  263. "AsyncIterator",
  264. "AsyncGenerator",
  265. "Hashable",
  266. "Iterable",
  267. "Iterator",
  268. "Generator",
  269. "Reversible",
  270. "Sized",
  271. "Container",
  272. "Callable",
  273. "Collection",
  274. "Set",
  275. "MutableSet",
  276. "Mapping",
  277. "MutableMapping",
  278. "MappingView",
  279. "KeysView",
  280. "ItemsView",
  281. "ValuesView",
  282. "Sequence",
  283. "MutableSequence",
  284. "ByteString",
  285. },
  286. },
  287. (3, 9, 0): {
  288. "smtpd": {
  289. "MailmanProxy",
  290. }
  291. },
  292. (3, 11, 0): {
  293. "typing": {
  294. "Text",
  295. },
  296. "webbrowser": {
  297. "MacOSX",
  298. },
  299. },
  300. }
  301. def _check_mode_str(mode: Any) -> bool:
  302. # check type
  303. if not isinstance(mode, str):
  304. return False
  305. # check syntax
  306. modes = set(mode)
  307. _mode = "rwatb+Ux"
  308. creating = "x" in modes
  309. if modes - set(_mode) or len(mode) > len(modes):
  310. return False
  311. # check logic
  312. reading = "r" in modes
  313. writing = "w" in modes
  314. appending = "a" in modes
  315. text = "t" in modes
  316. binary = "b" in modes
  317. if "U" in modes:
  318. if writing or appending or creating:
  319. return False
  320. reading = True
  321. if text and binary:
  322. return False
  323. total = reading + writing + appending + creating
  324. if total > 1:
  325. return False
  326. if not (reading or writing or appending or creating):
  327. return False
  328. return True
  329. class StdlibChecker(DeprecatedMixin, BaseChecker):
  330. name = "stdlib"
  331. msgs: dict[str, MessageDefinitionTuple] = {
  332. **DeprecatedMixin.DEPRECATED_METHOD_MESSAGE,
  333. **DeprecatedMixin.DEPRECATED_ARGUMENT_MESSAGE,
  334. **DeprecatedMixin.DEPRECATED_CLASS_MESSAGE,
  335. **DeprecatedMixin.DEPRECATED_DECORATOR_MESSAGE,
  336. "W1501": (
  337. '"%s" is not a valid mode for open.',
  338. "bad-open-mode",
  339. "Python supports: r, w, a[, x] modes with b, +, "
  340. "and U (only with r) options. "
  341. "See https://docs.python.org/3/library/functions.html#open",
  342. ),
  343. "W1502": (
  344. "Using datetime.time in a boolean context.",
  345. "boolean-datetime",
  346. "Using datetime.time in a boolean context can hide "
  347. "subtle bugs when the time they represent matches "
  348. "midnight UTC. This behaviour was fixed in Python 3.5. "
  349. "See https://bugs.python.org/issue13936 for reference.",
  350. {"maxversion": (3, 5)},
  351. ),
  352. "W1503": (
  353. "Redundant use of %s with constant value %r",
  354. "redundant-unittest-assert",
  355. "The first argument of assertTrue and assertFalse is "
  356. "a condition. If a constant is passed as parameter, that "
  357. "condition will be always true. In this case a warning "
  358. "should be emitted.",
  359. ),
  360. "W1506": (
  361. "threading.Thread needs the target function",
  362. "bad-thread-instantiation",
  363. "The warning is emitted when a threading.Thread class "
  364. "is instantiated without the target function being passed as a kwarg or as a second argument. "
  365. "By default, the first parameter is the group param, not the target param.",
  366. ),
  367. "W1507": (
  368. "Using copy.copy(os.environ). Use os.environ.copy() instead.",
  369. "shallow-copy-environ",
  370. "os.environ is not a dict object but proxy object, so "
  371. "shallow copy has still effects on original object. "
  372. "See https://bugs.python.org/issue15373 for reference.",
  373. ),
  374. "E1507": (
  375. "%s does not support %s type argument",
  376. "invalid-envvar-value",
  377. "Env manipulation functions support only string type arguments. "
  378. "See https://docs.python.org/3/library/os.html#os.getenv.",
  379. ),
  380. "E1519": (
  381. "singledispatch decorator should not be used with methods, "
  382. "use singledispatchmethod instead.",
  383. "singledispatch-method",
  384. "singledispatch should decorate functions and not class/instance methods. "
  385. "Use singledispatchmethod for those cases.",
  386. ),
  387. "E1520": (
  388. "singledispatchmethod decorator should not be used with functions, "
  389. "use singledispatch instead.",
  390. "singledispatchmethod-function",
  391. "singledispatchmethod should decorate class/instance methods and not functions. "
  392. "Use singledispatch for those cases.",
  393. ),
  394. "W1508": (
  395. "%s default type is %s. Expected str or None.",
  396. "invalid-envvar-default",
  397. "Env manipulation functions return None or str values. "
  398. "Supplying anything different as a default may cause bugs. "
  399. "See https://docs.python.org/3/library/os.html#os.getenv.",
  400. ),
  401. "W1509": (
  402. "Using preexec_fn keyword which may be unsafe in the presence "
  403. "of threads",
  404. "subprocess-popen-preexec-fn",
  405. "The preexec_fn parameter is not safe to use in the presence "
  406. "of threads in your application. The child process could "
  407. "deadlock before exec is called. If you must use it, keep it "
  408. "trivial! Minimize the number of libraries you call into. "
  409. "See https://docs.python.org/3/library/subprocess.html#popen-constructor",
  410. ),
  411. "W1510": (
  412. "'subprocess.run' used without explicitly defining the value for 'check'.",
  413. "subprocess-run-check",
  414. "The ``check`` keyword is set to False by default. It means the process "
  415. "launched by ``subprocess.run`` can exit with a non-zero exit code and "
  416. "fail silently. It's better to set it explicitly to make clear what the "
  417. "error-handling behavior is.",
  418. ),
  419. "W1514": (
  420. "Using open without explicitly specifying an encoding",
  421. "unspecified-encoding",
  422. "It is better to specify an encoding when opening documents. "
  423. "Using the system default implicitly can create problems on other operating systems. "
  424. "See https://peps.python.org/pep-0597/",
  425. ),
  426. "W1515": (
  427. "Leaving functions creating breakpoints in production code is not recommended",
  428. "forgotten-debug-statement",
  429. "Calls to breakpoint(), sys.breakpointhook() and pdb.set_trace() should be removed "
  430. "from code that is not actively being debugged.",
  431. ),
  432. "W1518": (
  433. "'lru_cache(maxsize=None)' or 'cache' will keep all method args alive indefinitely, including 'self'",
  434. "method-cache-max-size-none",
  435. "By decorating a method with lru_cache or cache the 'self' argument will be linked to "
  436. "the function and therefore never garbage collected. Unless your instance "
  437. "will never need to be garbage collected (singleton) it is recommended to refactor "
  438. "code to avoid this pattern or add a maxsize to the cache. "
  439. "The default value for maxsize is 128.",
  440. {
  441. "old_names": [
  442. ("W1516", "lru-cache-decorating-method"),
  443. ("W1517", "cache-max-size-none"),
  444. ]
  445. },
  446. ),
  447. }
  448. def __init__(self, linter: PyLinter) -> None:
  449. BaseChecker.__init__(self, linter)
  450. self._deprecated_methods: set[str] = set()
  451. self._deprecated_arguments: dict[str, tuple[tuple[int | None, str], ...]] = {}
  452. self._deprecated_classes: dict[str, set[str]] = {}
  453. self._deprecated_decorators: set[str] = set()
  454. for since_vers, func_list in DEPRECATED_METHODS[sys.version_info[0]].items():
  455. if since_vers <= sys.version_info:
  456. self._deprecated_methods.update(func_list)
  457. for since_vers, args_list in DEPRECATED_ARGUMENTS.items():
  458. if since_vers <= sys.version_info:
  459. self._deprecated_arguments.update(args_list)
  460. for since_vers, class_list in DEPRECATED_CLASSES.items():
  461. if since_vers <= sys.version_info:
  462. self._deprecated_classes.update(class_list)
  463. for since_vers, decorator_list in DEPRECATED_DECORATORS.items():
  464. if since_vers <= sys.version_info:
  465. self._deprecated_decorators.update(decorator_list)
  466. # Modules are checked by the ImportsChecker, because the list is
  467. # synced with the config argument deprecated-modules
  468. def _check_bad_thread_instantiation(self, node: nodes.Call) -> None:
  469. func_kwargs = {key.arg for key in node.keywords}
  470. if "target" in func_kwargs:
  471. return
  472. if len(node.args) < 2 and (not node.kwargs or "target" not in func_kwargs):
  473. self.add_message(
  474. "bad-thread-instantiation", node=node, confidence=interfaces.HIGH
  475. )
  476. def _check_for_preexec_fn_in_popen(self, node: nodes.Call) -> None:
  477. if node.keywords:
  478. for keyword in node.keywords:
  479. if keyword.arg == "preexec_fn":
  480. self.add_message("subprocess-popen-preexec-fn", node=node)
  481. def _check_for_check_kw_in_run(self, node: nodes.Call) -> None:
  482. kwargs = {keyword.arg for keyword in (node.keywords or ())}
  483. if "check" not in kwargs:
  484. self.add_message("subprocess-run-check", node=node, confidence=INFERENCE)
  485. def _check_shallow_copy_environ(self, node: nodes.Call) -> None:
  486. confidence = HIGH
  487. try:
  488. arg = utils.get_argument_from_call(node, position=0, keyword="x")
  489. except utils.NoSuchArgumentError:
  490. arg = utils.infer_kwarg_from_call(node, keyword="x")
  491. if not arg:
  492. return
  493. confidence = INFERENCE
  494. try:
  495. inferred_args = arg.inferred()
  496. except astroid.InferenceError:
  497. return
  498. for inferred in inferred_args:
  499. if inferred.qname() == OS_ENVIRON:
  500. self.add_message(
  501. "shallow-copy-environ", node=node, confidence=confidence
  502. )
  503. break
  504. @utils.only_required_for_messages(
  505. "bad-open-mode",
  506. "redundant-unittest-assert",
  507. "deprecated-method",
  508. "deprecated-argument",
  509. "bad-thread-instantiation",
  510. "shallow-copy-environ",
  511. "invalid-envvar-value",
  512. "invalid-envvar-default",
  513. "subprocess-popen-preexec-fn",
  514. "subprocess-run-check",
  515. "deprecated-class",
  516. "unspecified-encoding",
  517. "forgotten-debug-statement",
  518. )
  519. def visit_call(self, node: nodes.Call) -> None:
  520. """Visit a Call node."""
  521. self.check_deprecated_class_in_call(node)
  522. for inferred in utils.infer_all(node.func):
  523. if isinstance(inferred, util.UninferableBase):
  524. continue
  525. if inferred.root().name in OPEN_MODULE:
  526. open_func_name: str | None = None
  527. if isinstance(node.func, nodes.Name):
  528. open_func_name = node.func.name
  529. if isinstance(node.func, nodes.Attribute):
  530. open_func_name = node.func.attrname
  531. if open_func_name in OPEN_FILES_FUNCS:
  532. self._check_open_call(node, inferred.root().name, open_func_name)
  533. elif inferred.root().name == UNITTEST_CASE:
  534. self._check_redundant_assert(node, inferred)
  535. elif isinstance(inferred, nodes.ClassDef):
  536. if inferred.qname() == THREADING_THREAD:
  537. self._check_bad_thread_instantiation(node)
  538. elif inferred.qname() == SUBPROCESS_POPEN:
  539. self._check_for_preexec_fn_in_popen(node)
  540. elif isinstance(inferred, nodes.FunctionDef):
  541. name = inferred.qname()
  542. if name == COPY_COPY:
  543. self._check_shallow_copy_environ(node)
  544. elif name in ENV_GETTERS:
  545. self._check_env_function(node, inferred)
  546. elif name == SUBPROCESS_RUN:
  547. self._check_for_check_kw_in_run(node)
  548. elif name in DEBUG_BREAKPOINTS:
  549. self.add_message("forgotten-debug-statement", node=node)
  550. self.check_deprecated_method(node, inferred)
  551. @utils.only_required_for_messages("boolean-datetime")
  552. def visit_unaryop(self, node: nodes.UnaryOp) -> None:
  553. if node.op == "not":
  554. self._check_datetime(node.operand)
  555. @utils.only_required_for_messages("boolean-datetime")
  556. def visit_if(self, node: nodes.If) -> None:
  557. self._check_datetime(node.test)
  558. @utils.only_required_for_messages("boolean-datetime")
  559. def visit_ifexp(self, node: nodes.IfExp) -> None:
  560. self._check_datetime(node.test)
  561. @utils.only_required_for_messages("boolean-datetime")
  562. def visit_boolop(self, node: nodes.BoolOp) -> None:
  563. for value in node.values:
  564. self._check_datetime(value)
  565. @utils.only_required_for_messages(
  566. "method-cache-max-size-none",
  567. "singledispatch-method",
  568. "singledispatchmethod-function",
  569. )
  570. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  571. if node.decorators and isinstance(node.parent, nodes.ClassDef):
  572. self._check_lru_cache_decorators(node)
  573. self._check_dispatch_decorators(node)
  574. def _check_lru_cache_decorators(self, node: nodes.FunctionDef) -> None:
  575. """Check if instance methods are decorated with functools.lru_cache."""
  576. if any(utils.is_enum(ancestor) for ancestor in node.parent.ancestors()):
  577. # method of class inheriting from Enum is exempt from this check.
  578. return
  579. lru_cache_nodes: list[nodes.NodeNG] = []
  580. for d_node in node.decorators.nodes:
  581. # pylint: disable = too-many-try-statements
  582. try:
  583. for infered_node in d_node.infer():
  584. q_name = infered_node.qname()
  585. if q_name in NON_INSTANCE_METHODS:
  586. return
  587. # Check if there is a maxsize argument set to None in the call
  588. if q_name in LRU_CACHE and isinstance(d_node, nodes.Call):
  589. try:
  590. arg = utils.get_argument_from_call(
  591. d_node, position=0, keyword="maxsize"
  592. )
  593. except utils.NoSuchArgumentError:
  594. break
  595. if not isinstance(arg, nodes.Const) or arg.value is not None:
  596. break
  597. lru_cache_nodes.append(d_node)
  598. break
  599. if q_name == "functools.cache":
  600. lru_cache_nodes.append(d_node)
  601. break
  602. except astroid.InferenceError:
  603. pass
  604. for lru_cache_node in lru_cache_nodes:
  605. self.add_message(
  606. "method-cache-max-size-none",
  607. node=lru_cache_node,
  608. confidence=interfaces.INFERENCE,
  609. )
  610. def _check_dispatch_decorators(self, node: nodes.FunctionDef) -> None:
  611. decorators_map: dict[str, tuple[nodes.NodeNG, interfaces.Confidence]] = {}
  612. for decorator in node.decorators.nodes:
  613. if isinstance(decorator, nodes.Name) and decorator.name:
  614. decorators_map[decorator.name] = (decorator, interfaces.HIGH)
  615. elif utils.is_registered_in_singledispatch_function(node):
  616. decorators_map["singledispatch"] = (decorator, interfaces.INFERENCE)
  617. elif utils.is_registered_in_singledispatchmethod_function(node):
  618. decorators_map["singledispatchmethod"] = (
  619. decorator,
  620. interfaces.INFERENCE,
  621. )
  622. if "singledispatch" in decorators_map and "classmethod" in decorators_map:
  623. self.add_message(
  624. "singledispatch-method",
  625. node=decorators_map["singledispatch"][0],
  626. confidence=decorators_map["singledispatch"][1],
  627. )
  628. elif (
  629. "singledispatchmethod" in decorators_map
  630. and "staticmethod" in decorators_map
  631. ):
  632. self.add_message(
  633. "singledispatchmethod-function",
  634. node=decorators_map["singledispatchmethod"][0],
  635. confidence=decorators_map["singledispatchmethod"][1],
  636. )
  637. def _check_redundant_assert(self, node: nodes.Call, infer: InferenceResult) -> None:
  638. if (
  639. isinstance(infer, astroid.BoundMethod)
  640. and node.args
  641. and isinstance(node.args[0], nodes.Const)
  642. and infer.name in {"assertTrue", "assertFalse"}
  643. ):
  644. self.add_message(
  645. "redundant-unittest-assert",
  646. args=(infer.name, node.args[0].value),
  647. node=node,
  648. )
  649. def _check_datetime(self, node: nodes.NodeNG) -> None:
  650. """Check that a datetime was inferred, if so, emit boolean-datetime warning."""
  651. try:
  652. inferred = next(node.infer())
  653. except astroid.InferenceError:
  654. return
  655. if (
  656. isinstance(inferred, astroid.Instance)
  657. and inferred.qname() == "datetime.time"
  658. ):
  659. self.add_message("boolean-datetime", node=node)
  660. def _check_open_call(
  661. self, node: nodes.Call, open_module: str, func_name: str
  662. ) -> None:
  663. """Various checks for an open call."""
  664. mode_arg = None
  665. confidence = HIGH
  666. try:
  667. if open_module == "_io":
  668. mode_arg = utils.get_argument_from_call(
  669. node, position=1, keyword="mode"
  670. )
  671. elif open_module == "pathlib":
  672. mode_arg = utils.get_argument_from_call(
  673. node, position=0, keyword="mode"
  674. )
  675. except utils.NoSuchArgumentError:
  676. mode_arg = utils.infer_kwarg_from_call(node, keyword="mode")
  677. if mode_arg:
  678. confidence = INFERENCE
  679. if mode_arg:
  680. mode_arg = utils.safe_infer(mode_arg)
  681. if (
  682. func_name in OPEN_FILES_MODE
  683. and isinstance(mode_arg, nodes.Const)
  684. and not _check_mode_str(mode_arg.value)
  685. ):
  686. self.add_message(
  687. "bad-open-mode",
  688. node=node,
  689. args=mode_arg.value or str(mode_arg.value),
  690. confidence=confidence,
  691. )
  692. if (
  693. not mode_arg
  694. or isinstance(mode_arg, nodes.Const)
  695. and (not mode_arg.value or "b" not in str(mode_arg.value))
  696. ):
  697. confidence = HIGH
  698. try:
  699. if open_module == "pathlib":
  700. if node.func.attrname == "read_text":
  701. encoding_arg = utils.get_argument_from_call(
  702. node, position=0, keyword="encoding"
  703. )
  704. elif node.func.attrname == "write_text":
  705. encoding_arg = utils.get_argument_from_call(
  706. node, position=1, keyword="encoding"
  707. )
  708. else:
  709. encoding_arg = utils.get_argument_from_call(
  710. node, position=2, keyword="encoding"
  711. )
  712. else:
  713. encoding_arg = utils.get_argument_from_call(
  714. node, position=3, keyword="encoding"
  715. )
  716. except utils.NoSuchArgumentError:
  717. encoding_arg = utils.infer_kwarg_from_call(node, keyword="encoding")
  718. if encoding_arg:
  719. confidence = INFERENCE
  720. else:
  721. self.add_message(
  722. "unspecified-encoding", node=node, confidence=confidence
  723. )
  724. if encoding_arg:
  725. encoding_arg = utils.safe_infer(encoding_arg)
  726. if isinstance(encoding_arg, nodes.Const) and encoding_arg.value is None:
  727. self.add_message(
  728. "unspecified-encoding", node=node, confidence=confidence
  729. )
  730. def _check_env_function(self, node: nodes.Call, infer: nodes.FunctionDef) -> None:
  731. env_name_kwarg = "key"
  732. env_value_kwarg = "default"
  733. if node.keywords:
  734. kwargs = {keyword.arg: keyword.value for keyword in node.keywords}
  735. else:
  736. kwargs = None
  737. if node.args:
  738. env_name_arg = node.args[0]
  739. elif kwargs and env_name_kwarg in kwargs:
  740. env_name_arg = kwargs[env_name_kwarg]
  741. else:
  742. env_name_arg = None
  743. if env_name_arg:
  744. self._check_invalid_envvar_value(
  745. node=node,
  746. message="invalid-envvar-value",
  747. call_arg=utils.safe_infer(env_name_arg),
  748. infer=infer,
  749. allow_none=False,
  750. )
  751. if len(node.args) == 2:
  752. env_value_arg = node.args[1]
  753. elif kwargs and env_value_kwarg in kwargs:
  754. env_value_arg = kwargs[env_value_kwarg]
  755. else:
  756. env_value_arg = None
  757. if env_value_arg:
  758. self._check_invalid_envvar_value(
  759. node=node,
  760. infer=infer,
  761. message="invalid-envvar-default",
  762. call_arg=utils.safe_infer(env_value_arg),
  763. allow_none=True,
  764. )
  765. def _check_invalid_envvar_value(
  766. self,
  767. node: nodes.Call,
  768. infer: nodes.FunctionDef,
  769. message: str,
  770. call_arg: InferenceResult | None,
  771. allow_none: bool,
  772. ) -> None:
  773. if call_arg is None or isinstance(call_arg, util.UninferableBase):
  774. return
  775. name = infer.qname()
  776. if isinstance(call_arg, nodes.Const):
  777. emit = False
  778. if call_arg.value is None:
  779. emit = not allow_none
  780. elif not isinstance(call_arg.value, str):
  781. emit = True
  782. if emit:
  783. self.add_message(message, node=node, args=(name, call_arg.pytype()))
  784. else:
  785. self.add_message(message, node=node, args=(name, call_arg.pytype()))
  786. def deprecated_methods(self) -> set[str]:
  787. return self._deprecated_methods
  788. def deprecated_arguments(self, method: str) -> tuple[tuple[int | None, str], ...]:
  789. return self._deprecated_arguments.get(method, ())
  790. def deprecated_classes(self, module: str) -> Iterable[str]:
  791. return self._deprecated_classes.get(module, ())
  792. def deprecated_decorators(self) -> Iterable[str]:
  793. return self._deprecated_decorators
  794. def register(linter: PyLinter) -> None:
  795. linter.register_checker(StdlibChecker(linter))