stdlib.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/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 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. arg = utils.get_argument_from_call(node, position=0)
  487. try:
  488. inferred_args = arg.inferred()
  489. except astroid.InferenceError:
  490. return
  491. for inferred in inferred_args:
  492. if inferred.qname() == OS_ENVIRON:
  493. self.add_message("shallow-copy-environ", node=node)
  494. break
  495. @utils.only_required_for_messages(
  496. "bad-open-mode",
  497. "redundant-unittest-assert",
  498. "deprecated-method",
  499. "deprecated-argument",
  500. "bad-thread-instantiation",
  501. "shallow-copy-environ",
  502. "invalid-envvar-value",
  503. "invalid-envvar-default",
  504. "subprocess-popen-preexec-fn",
  505. "subprocess-run-check",
  506. "deprecated-class",
  507. "unspecified-encoding",
  508. "forgotten-debug-statement",
  509. )
  510. def visit_call(self, node: nodes.Call) -> None:
  511. """Visit a Call node."""
  512. self.check_deprecated_class_in_call(node)
  513. for inferred in utils.infer_all(node.func):
  514. if isinstance(inferred, util.UninferableBase):
  515. continue
  516. if inferred.root().name in OPEN_MODULE:
  517. open_func_name: str | None = None
  518. if isinstance(node.func, nodes.Name):
  519. open_func_name = node.func.name
  520. if isinstance(node.func, nodes.Attribute):
  521. open_func_name = node.func.attrname
  522. if open_func_name in OPEN_FILES_FUNCS:
  523. self._check_open_call(node, inferred.root().name, open_func_name)
  524. elif inferred.root().name == UNITTEST_CASE:
  525. self._check_redundant_assert(node, inferred)
  526. elif isinstance(inferred, nodes.ClassDef):
  527. if inferred.qname() == THREADING_THREAD:
  528. self._check_bad_thread_instantiation(node)
  529. elif inferred.qname() == SUBPROCESS_POPEN:
  530. self._check_for_preexec_fn_in_popen(node)
  531. elif isinstance(inferred, nodes.FunctionDef):
  532. name = inferred.qname()
  533. if name == COPY_COPY:
  534. self._check_shallow_copy_environ(node)
  535. elif name in ENV_GETTERS:
  536. self._check_env_function(node, inferred)
  537. elif name == SUBPROCESS_RUN:
  538. self._check_for_check_kw_in_run(node)
  539. elif name in DEBUG_BREAKPOINTS:
  540. self.add_message("forgotten-debug-statement", node=node)
  541. self.check_deprecated_method(node, inferred)
  542. @utils.only_required_for_messages("boolean-datetime")
  543. def visit_unaryop(self, node: nodes.UnaryOp) -> None:
  544. if node.op == "not":
  545. self._check_datetime(node.operand)
  546. @utils.only_required_for_messages("boolean-datetime")
  547. def visit_if(self, node: nodes.If) -> None:
  548. self._check_datetime(node.test)
  549. @utils.only_required_for_messages("boolean-datetime")
  550. def visit_ifexp(self, node: nodes.IfExp) -> None:
  551. self._check_datetime(node.test)
  552. @utils.only_required_for_messages("boolean-datetime")
  553. def visit_boolop(self, node: nodes.BoolOp) -> None:
  554. for value in node.values:
  555. self._check_datetime(value)
  556. @utils.only_required_for_messages(
  557. "method-cache-max-size-none",
  558. "singledispatch-method",
  559. "singledispatchmethod-function",
  560. )
  561. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  562. if node.decorators and isinstance(node.parent, nodes.ClassDef):
  563. self._check_lru_cache_decorators(node)
  564. self._check_dispatch_decorators(node)
  565. def _check_lru_cache_decorators(self, node: nodes.FunctionDef) -> None:
  566. """Check if instance methods are decorated with functools.lru_cache."""
  567. if any(utils.is_enum(ancestor) for ancestor in node.parent.ancestors()):
  568. # method of class inheriting from Enum is exempt from this check.
  569. return
  570. lru_cache_nodes: list[nodes.NodeNG] = []
  571. for d_node in node.decorators.nodes:
  572. # pylint: disable = too-many-try-statements
  573. try:
  574. for infered_node in d_node.infer():
  575. q_name = infered_node.qname()
  576. if q_name in NON_INSTANCE_METHODS:
  577. return
  578. # Check if there is a maxsize argument set to None in the call
  579. if q_name in LRU_CACHE and isinstance(d_node, nodes.Call):
  580. try:
  581. arg = utils.get_argument_from_call(
  582. d_node, position=0, keyword="maxsize"
  583. )
  584. except utils.NoSuchArgumentError:
  585. break
  586. if not isinstance(arg, nodes.Const) or arg.value is not None:
  587. break
  588. lru_cache_nodes.append(d_node)
  589. break
  590. if q_name == "functools.cache":
  591. lru_cache_nodes.append(d_node)
  592. break
  593. except astroid.InferenceError:
  594. pass
  595. for lru_cache_node in lru_cache_nodes:
  596. self.add_message(
  597. "method-cache-max-size-none",
  598. node=lru_cache_node,
  599. confidence=interfaces.INFERENCE,
  600. )
  601. def _check_dispatch_decorators(self, node: nodes.FunctionDef) -> None:
  602. decorators_map: dict[str, tuple[nodes.NodeNG, interfaces.Confidence]] = {}
  603. for decorator in node.decorators.nodes:
  604. if isinstance(decorator, nodes.Name) and decorator.name:
  605. decorators_map[decorator.name] = (decorator, interfaces.HIGH)
  606. elif utils.is_registered_in_singledispatch_function(node):
  607. decorators_map["singledispatch"] = (decorator, interfaces.INFERENCE)
  608. elif utils.is_registered_in_singledispatchmethod_function(node):
  609. decorators_map["singledispatchmethod"] = (
  610. decorator,
  611. interfaces.INFERENCE,
  612. )
  613. if "singledispatch" in decorators_map and "classmethod" in decorators_map:
  614. self.add_message(
  615. "singledispatch-method",
  616. node=decorators_map["singledispatch"][0],
  617. confidence=decorators_map["singledispatch"][1],
  618. )
  619. elif (
  620. "singledispatchmethod" in decorators_map
  621. and "staticmethod" in decorators_map
  622. ):
  623. self.add_message(
  624. "singledispatchmethod-function",
  625. node=decorators_map["singledispatchmethod"][0],
  626. confidence=decorators_map["singledispatchmethod"][1],
  627. )
  628. def _check_redundant_assert(self, node: nodes.Call, infer: InferenceResult) -> None:
  629. if (
  630. isinstance(infer, astroid.BoundMethod)
  631. and node.args
  632. and isinstance(node.args[0], nodes.Const)
  633. and infer.name in {"assertTrue", "assertFalse"}
  634. ):
  635. self.add_message(
  636. "redundant-unittest-assert",
  637. args=(infer.name, node.args[0].value),
  638. node=node,
  639. )
  640. def _check_datetime(self, node: nodes.NodeNG) -> None:
  641. """Check that a datetime was inferred, if so, emit boolean-datetime warning."""
  642. try:
  643. inferred = next(node.infer())
  644. except astroid.InferenceError:
  645. return
  646. if (
  647. isinstance(inferred, astroid.Instance)
  648. and inferred.qname() == "datetime.time"
  649. ):
  650. self.add_message("boolean-datetime", node=node)
  651. def _check_open_call(
  652. self, node: nodes.Call, open_module: str, func_name: str
  653. ) -> None:
  654. """Various checks for an open call."""
  655. mode_arg = None
  656. try:
  657. if open_module == "_io":
  658. mode_arg = utils.get_argument_from_call(
  659. node, position=1, keyword="mode"
  660. )
  661. elif open_module == "pathlib":
  662. mode_arg = utils.get_argument_from_call(
  663. node, position=0, keyword="mode"
  664. )
  665. except utils.NoSuchArgumentError:
  666. pass
  667. if mode_arg:
  668. mode_arg = utils.safe_infer(mode_arg)
  669. if (
  670. func_name in OPEN_FILES_MODE
  671. and isinstance(mode_arg, nodes.Const)
  672. and not _check_mode_str(mode_arg.value)
  673. ):
  674. self.add_message(
  675. "bad-open-mode",
  676. node=node,
  677. args=mode_arg.value or str(mode_arg.value),
  678. )
  679. if (
  680. not mode_arg
  681. or isinstance(mode_arg, nodes.Const)
  682. and (not mode_arg.value or "b" not in str(mode_arg.value))
  683. ):
  684. encoding_arg = None
  685. try:
  686. if open_module == "pathlib":
  687. if node.func.attrname == "read_text":
  688. encoding_arg = utils.get_argument_from_call(
  689. node, position=0, keyword="encoding"
  690. )
  691. elif node.func.attrname == "write_text":
  692. encoding_arg = utils.get_argument_from_call(
  693. node, position=1, keyword="encoding"
  694. )
  695. else:
  696. encoding_arg = utils.get_argument_from_call(
  697. node, position=2, keyword="encoding"
  698. )
  699. else:
  700. encoding_arg = utils.get_argument_from_call(
  701. node, position=3, keyword="encoding"
  702. )
  703. except utils.NoSuchArgumentError:
  704. self.add_message("unspecified-encoding", node=node)
  705. if encoding_arg:
  706. encoding_arg = utils.safe_infer(encoding_arg)
  707. if isinstance(encoding_arg, nodes.Const) and encoding_arg.value is None:
  708. self.add_message("unspecified-encoding", node=node)
  709. def _check_env_function(self, node: nodes.Call, infer: nodes.FunctionDef) -> None:
  710. env_name_kwarg = "key"
  711. env_value_kwarg = "default"
  712. if node.keywords:
  713. kwargs = {keyword.arg: keyword.value for keyword in node.keywords}
  714. else:
  715. kwargs = None
  716. if node.args:
  717. env_name_arg = node.args[0]
  718. elif kwargs and env_name_kwarg in kwargs:
  719. env_name_arg = kwargs[env_name_kwarg]
  720. else:
  721. env_name_arg = None
  722. if env_name_arg:
  723. self._check_invalid_envvar_value(
  724. node=node,
  725. message="invalid-envvar-value",
  726. call_arg=utils.safe_infer(env_name_arg),
  727. infer=infer,
  728. allow_none=False,
  729. )
  730. if len(node.args) == 2:
  731. env_value_arg = node.args[1]
  732. elif kwargs and env_value_kwarg in kwargs:
  733. env_value_arg = kwargs[env_value_kwarg]
  734. else:
  735. env_value_arg = None
  736. if env_value_arg:
  737. self._check_invalid_envvar_value(
  738. node=node,
  739. infer=infer,
  740. message="invalid-envvar-default",
  741. call_arg=utils.safe_infer(env_value_arg),
  742. allow_none=True,
  743. )
  744. def _check_invalid_envvar_value(
  745. self,
  746. node: nodes.Call,
  747. infer: nodes.FunctionDef,
  748. message: str,
  749. call_arg: InferenceResult | None,
  750. allow_none: bool,
  751. ) -> None:
  752. if call_arg is None or isinstance(call_arg, util.UninferableBase):
  753. return
  754. name = infer.qname()
  755. if isinstance(call_arg, nodes.Const):
  756. emit = False
  757. if call_arg.value is None:
  758. emit = not allow_none
  759. elif not isinstance(call_arg.value, str):
  760. emit = True
  761. if emit:
  762. self.add_message(message, node=node, args=(name, call_arg.pytype()))
  763. else:
  764. self.add_message(message, node=node, args=(name, call_arg.pytype()))
  765. def deprecated_methods(self) -> set[str]:
  766. return self._deprecated_methods
  767. def deprecated_arguments(self, method: str) -> tuple[tuple[int | None, str], ...]:
  768. return self._deprecated_arguments.get(method, ())
  769. def deprecated_classes(self, module: str) -> Iterable[str]:
  770. return self._deprecated_classes.get(module, ())
  771. def deprecated_decorators(self) -> Iterable[str]:
  772. return self._deprecated_decorators
  773. def register(linter: PyLinter) -> None:
  774. linter.register_checker(StdlibChecker(linter))