with
statements can be merged PTC-W0062With
statements`21 headers = {"user-agent": "httpie"}
22 url = f"https://wttr.in/{location}?mnTC0&lang=en"
23 try:
24 async with aiohttp.ClientSession(headers=headers) as session:25 async with session.get(url) as resp:
26 data = await resp.text()
27 except Exception:
With
statements`20 await message.delete()
21 return
22
23 async with aiohttp.ClientSession() as session:24 async with session.get(
25 f"https://api.funtranslations.com/translate/yoda.json?text={txt}"
26 ) as resp:
With
statements`12@UserBot.on_message(filters.command(["pat", "pats"], ".") & filters.me)
13async def give_pats(bot: UserBot, message: Message):
14 URL = "https://some-random-api.com/animu/pat"
15 async with aiohttp.ClientSession() as session:16 async with session.get(URL) as request:
17 if request.status == 404:
18 return await message.edit("`no Pats for you :c")
With
statements`14async def paste(bot: UserBot, message: Message):
15 text = message.reply_to_message.text
16 try:
17 async with aiohttp.ClientSession() as session:18 async with session.post(
19 "https://nekobin.com/api/documents", json={"content": text}, timeout=3
20 ) as response:
With
statements` 4class Spacex:
5 @staticmethod
6 async def get(latest_or_next):
7 async with aiohttp.ClientSession() as session: 8 async with session.get(
9 f"https://api.spacexdata.com/v3/launches/{latest_or_next}"
10 ) as resp:
Nested with
statements can be merged into a single compound with
statement.
This issue is raised only when there are no other statements between the nested with
statements.
Merging collapsible with
statements can decrease cognitive complexity, improving code readability.
with open("file1", "w") as file1:
with open("file2", "w") as file2:
doSomething()
with open("file1", "w") as file1, open("file2", "w") as file2:
doSomething()