Skip to content

Typing: add explicit types for public fields - #2469

Closed
adriangb wants to merge 12 commits into
encode:masterfrom
adriangb:explicit-field-types
Closed

adriangb wants to merge 12 commits into
encode:masterfrom
adriangb:explicit-field-types

Conversation

@adriangb

@adriangb adriangb commented Nov 29, 2022 •

Copy link
Copy Markdown
Contributor

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

mjpieters and others added 4 commits November 29, 2022 00:38
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.
This reverts commit 1501725.
@adriangb adriangb mentioned this pull request Nov 29, 2022
@lovelydinosaur

Copy link
Copy Markdown
Member

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.

@adriangb

Copy link
Copy Markdown
Contributor Author

Fair enough, happy to pause for now

@mjpieters

Copy link
Copy Markdown
Contributor
  • 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.

@adriangb

Copy link
Copy Markdown
Contributor Author

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).

Comment thread httpx/_transports/wsgi.py
Comment on lines +83 to +87
app: typing.Callable[..., typing.Any]
raise_app_exceptions: bool
script_name: str
remote_addr: str
wsgi_errors: typing.Optional[typing.TextIO]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really want these to be public API.

The HTTPTransport class has it's implementation attributes use a private namespace, but the WSGITransport and ASGITransport classes currently don't.

Comment thread httpx/_models.py
Comment on lines +1026 to +1027
jar: CookieJar

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. Restructuring __init__ would probably stop this being ambiguous to pyright.

The __init__ method should probably start with self.jar = CookieJar() before the branching cases. That'd seem easier to reason about from my perspective, as well as from pyright's.

Comment thread httpx/_models.py
Comment on lines +308 to +312
method: str
url: URL
headers: Headers
extensions: RequestExtensions
stream: typing.Union[SyncByteStream, AsyncByteStream]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which of these are ambiguous to pyright? Some? All?

Comment thread httpx/_models.py
Comment on lines +452 to +460
status_code: int
headers: Headers
next_request: typing.Optional[Request]
extensions: ResponseExtensions
history: typing.List["Response"]
is_closed: bool
is_stream_consumed: bool
default_encoding: typing.Union[str, typing.Callable[[bytes], str]]
stream: typing.Union[SyncByteStream, AsyncByteStream]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which of these are ambiguous to pyright?

@lovelydinosaur

Copy link
Copy Markdown
Member

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 lovelydinosaur added the refactor Issues and PRs related to code refactoring label Jan 4, 2023
@lovelydinosaur

Copy link
Copy Markdown
Member

Going to close this off as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Issues and PRs related to code refactoring

Development

Successfully merging this pull request may close these issues.

3 participants