httpx icon indicating copy to clipboard operation
httpx copied to clipboard

Typing: add explicit types for public fields

Open adriangb opened this issue 2 years ago • 5 comments
trafficstars

Without these, type inference would be required, the output of which can differ between type checkers. Explict fields also serves as documentation and can be a place to add explicit field descriptions in future.

pyright reports: after.txt before.txt

adriangb avatar Nov 29 '22 06:11 adriangb

So, I'm less convinced about this one...

  • We don't have any tooling at the moment that prevents anything from regressing.
  • It doesn't look consistent to me. The change is on Request and Response and Cookies, but there's several other classes that it could equally well apply too?

Personally my suggestion would be that we punt on any pyright based changes, until we've completed the mypy --strict config, by tackling the issues in #2436 one-by-one.

lovelydinosaur avatar Nov 29 '22 17:11 lovelydinosaur

Fair enough, happy to pause for now

adriangb avatar Nov 29 '22 17:11 adriangb

  • We don't have any tooling at the moment that prevents anything from regressing.

That could be pyright --verifytypes.

  • It doesn't look consistent to me. The change is on Request and Response and Cookies, but there's several other classes that it could equally well apply too?

pyright --verifytypes looks at the exported types and then only those entries that are ambiguous due to inference rules being inconsistent.

mjpieters avatar Nov 29 '22 21:11 mjpieters

I was thinking about what sorts of changes can happen and what would get caught or not.

1. New field added to

A new parameter is added to __init__ that becomes a field. It's inferred correctly by mypy and we don't add it. Most users aren't impacted but these explicit fields slowly loose value as documentation.

2. Type of field changed in __init__

This would immediately be caught by mypy or any other type checker as soon as we try to assign to it.

3. Field deleted

This would not be caught by any type checker that I know of and if we don't access the attribute in tests we'd be shipping a bug (we promised it exists but doesn't).

adriangb avatar Nov 29 '22 21:11 adriangb

If y'all are keen on having pyright pass cleanly, maybe we should start out with a discussion or issue similar to #2436, so we can make sure to tackle this fully.

lovelydinosaur avatar Dec 12 '22 12:12 lovelydinosaur

Going to close this off as stale.

lovelydinosaur avatar Mar 01 '24 15:03 lovelydinosaur