Typing: Correctly set types when getting `Record` fields
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start with the conn.fetch(..., record_class=Row) example and the asyncpg.Record field access shown in the issue. Investigate how the declared Row.num annotation could affect the inferred type of row["num"]. Done means the example's num is inferred as int and no longer produces mypy's Any return error.
Written by the indexing model from the issue text.
Description
Consider the following:
import asyncio
import asyncpg
async def main() -> int:
connection_string = "postgresql://postgres:postgres@127.0.0.1:5432/postgres"
conn = await asyncpg.connect(connection_string)
result = await conn.fetch("SELECT 1 + 1 AS num", record_class=Row)
row = result[0]
num = row["num"]
print(type(num))
return num # mypy error: Returning Any from function declared to return "int"
class Row(asyncpg.Record):
num: int
if __name__ == "__main__":
asyncio.run(main())
In essence, the type of num is Any instead of int. I tried to make Row inherit from TypedDict or use a dataclass, both failed.
I'll also be happy to help develop this feature with some general guidance.
- Dominant language
- Python
- Stars
- 8.1k
- Forks
- 469
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 9
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MagicStack/asyncpg
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
MagicStack/asyncpg#1357 ·
-
tests fail in 2032 Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
MagicStack/asyncpg#997 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
MagicStack/asyncpg#1354 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
MagicStack/asyncpg#1342 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 56/100
MagicStack/asyncpg#1340 · 1 comment ·
All issues in MagicStack/asyncpg
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
syfoud/Simulated_Scepter#172 ·
-
A cancelled tests run makes the coverage comment workflow fail and reports it as a red check on main Openarea: ci bug perceived difficulty: 3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Nitjsefnie-Harness-Commons/daedalus#921 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
EleutherAI/lm-evaluation-harness#4207 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-connect#1057 ·