Sample

pyint-d29aaddf0b01-I01

Remove comments and docstrings

Matched original source

Lines435

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn0.0080–1; higher is more readable
Mi0.6390–1; higher is more readable
LLOC165Logical lines; fewer is more readable
DeepSeek V4 Pro16.330–20; mean of three runs
GPT-6.1 Sol140–20; mean of three runs
BTTF0.4020–1; higher is more readable

Views

Source code

from __future__ import annotations import os import sys import time from collections import OrderedDict from collections.abc import Generator, Mapping, MutableMapping from datetime import timedelta from typing import TYPE_CHECKING, Any, cast from ._internal_utils import to_native_string from ._types import is_prepared as _is_prepared from .adapters import HTTPAdapter from .auth import _basic_auth_str from .compat import cookielib, urljoin, urlparse from .cookies import ( RequestsCookieJar, cookiejar_from_dict, extract_cookies_to_jar, merge_cookies, ) from .exceptions import ( ChunkedEncodingError, ContentDecodingError, InvalidSchema, TooManyRedirects, ) from .hooks import default_hooks, dispatch_hook from .models import ( DEFAULT_REDIRECT_LIMIT, REDIRECT_STATI, PreparedRequest, Request, Response, ) from .status_codes import codes from .structures import CaseInsensitiveDict from .utils import ( DEFAULT_PORTS, default_headers, get_auth_from_url, get_environ_proxies, get_netrc_auth, requote_uri, resolve_proxies, rewind_body, should_bypass_proxies, to_key_val_list, ) class Session(SessionRedirectMixin): headers: CaseInsensitiveDict[str] auth: _t.AuthType proxies: dict[str, str] hooks: dict[str, list[_t.HookType]] params: MutableMapping[str, Any] stream: bool verify: _t.VerifyType cert: _t.CertType max_redirects: int trust_env: bool cookies: RequestsCookieJar adapters: MutableMapping[str, BaseAdapter] __attrs__: list[str] = [ "headers", "cookies", "auth", "proxies", "hooks", "params", "verify", "cert", "adapters", "stream", "trust_env", "max_redirects", ] def __init__(self) -> None: self.headers = default_headers() self.auth = None self.proxies = {} self.hooks = default_hooks() self.params = {} self.stream = False self.verify = True self.cert = None self.max_redirects = DEFAULT_REDIRECT_LIMIT self.trust_env = True self.cookies = cookiejar_from_dict({}) self.adapters = OrderedDict() self.mount("https://", HTTPAdapter()) self.mount("http://", HTTPAdapter()) def __enter__(self) -> Self: return self def __exit__(self, *args: Any) -> None: self.close() def prepare_request(self, request: Request) -> PreparedRequest: url = cast("_t.UriType", request.url) method = cast(str, request.method) cookies = request.cookies or {} if not isinstance(cookies, cookielib.CookieJar): cookies = cookiejar_from_dict(cookies) merged_cookies = merge_cookies( merge_cookies(RequestsCookieJar(), self.cookies), cookies ) auth = request.auth if self.trust_env and not auth and not self.auth: auth = get_netrc_auth(url) p = PreparedRequest() p.prepare( method=method.upper(), url=url, files=request.files, data=request.data, json=request.json, headers=merge_setting( request.headers, self.headers, dict_class=CaseInsensitiveDict ), params=merge_setting(request.params, self.params), auth=merge_setting(auth, self.auth), cookies=merged_cookies, hooks=merge_hooks(request.hooks, self.hooks), ) return p def request( self, method: str, url: _t.UriType, params: _t.ParamsType = None, data: _t.DataType = None, headers: _t.HeadersType = None, cookies: RequestsCookieJar | CookieJar | dict[str, str] | None = None, files: _t.FilesType = None, auth: _t.AuthType = None, timeout: _t.TimeoutType = None, allow_redirects: bool = True, proxies: dict[str, str] | None = None, hooks: _t.HooksInputType | None = None, stream: bool | None = None, verify: _t.VerifyType | None = None, cert: _t.CertType = None, json: _t.JsonType = None, ) -> Response: if isinstance(url, bytes): url = url.decode("utf-8") req = Request( method=method.upper(), url=url, headers=headers, files=files, data=data or {}, json=json, params=params or {}, auth=auth, cookies=cookies, hooks=hooks, ) prep = self.prepare_request(req) assert _is_prepared(prep) proxies = proxies or {} settings = self.merge_environment_settings( prep.url, proxies, stream, verify, cert ) send_kwargs = { "timeout": timeout, "allow_redirects": allow_redirects, } send_kwargs.update(settings) resp = self.send(prep, **send_kwargs) return resp def get( self, url: _t.UriType, params: _t.ParamsType = None, **kwargs: Unpack[_t.GetKwargs], ) -> Response: kwargs.setdefault("allow_redirects", True) return self.request("GET", url, params=params, **kwargs) def options(self, url: _t.UriType, **kwargs: Unpack[_t.RequestKwargs]) -> Response: kwargs.setdefault("allow_redirects", True) return self.request("OPTIONS", url, **kwargs) def head(self, url: _t.UriType, **kwargs: Unpack[_t.RequestKwargs]) -> Response: kwargs.setdefault("allow_redirects", False) return self.request("HEAD", url, **kwargs) def post( self, url: _t.UriType, data: _t.DataType = None, json: _t.JsonType = None, **kwargs: Unpack[_t.PostKwargs], ) -> Response: return self.request("POST", url, data=data, json=json, **kwargs) def put( self, url: _t.UriType, data: _t.DataType = None, **kwargs: Unpack[_t.DataKwargs] ) -> Response: return self.request("PUT", url, data=data, **kwargs) def patch( self, url: _t.UriType, data: _t.DataType = None, **kwargs: Unpack[_t.DataKwargs] ) -> Response: return self.request("PATCH", url, data=data, **kwargs) def delete(self, url: _t.UriType, **kwargs: Unpack[_t.RequestKwargs]) -> Response: return self.request("DELETE", url, **kwargs) def send(self, request: PreparedRequest, **kwargs: Any) -> Response: kwargs.setdefault("stream", self.stream) kwargs.setdefault("verify", self.verify) kwargs.setdefault("cert", self.cert) if "proxies" not in kwargs: kwargs["proxies"] = resolve_proxies(request, self.proxies, self.trust_env) if isinstance(request, Request): raise ValueError("You can only send PreparedRequests.") assert _is_prepared(request) allow_redirects = kwargs.pop("allow_redirects", True) stream = kwargs.get("stream") hooks = request.hooks adapter = self.get_adapter(url=request.url) start = preferred_clock() r = adapter.send(request, **kwargs) elapsed = preferred_clock() - start r.elapsed = timedelta(seconds=elapsed) r = dispatch_hook("response", hooks, r, **kwargs) if r.history: for resp in r.history: extract_cookies_to_jar(self.cookies, resp.request, resp.raw) extract_cookies_to_jar(self.cookies, request, r.raw) if allow_redirects: gen = self.resolve_redirects(r, request, **kwargs) history = [resp for resp in gen] else: history = [] if history: history.insert(0, r) r = history.pop() r.history = history if not allow_redirects: try: r._next = next( self.resolve_redirects(r, request, yield_requests=True, **kwargs) ) except StopIteration: pass if not stream: r.content return r def merge_environment_settings( self, url: str, proxies: dict[str, str] | None, stream: bool | None, verify: _t.VerifyType | None, cert: _t.CertType, ) -> dict[str, Any]: if self.trust_env: no_proxy = proxies.get("no_proxy") if proxies is not None else None env_proxies = get_environ_proxies(url, no_proxy=no_proxy) if proxies is not None: for k, v in env_proxies.items(): proxies.setdefault(k, v) if verify is True or verify is None: verify = ( os.environ.get("REQUESTS_CA_BUNDLE") or os.environ.get("CURL_CA_BUNDLE") or verify ) proxies = merge_setting(proxies, self.proxies) stream = merge_setting(stream, self.stream) verify = merge_setting(verify, self.verify) cert = merge_setting(cert, self.cert) return {"proxies": proxies, "stream": stream, "verify": verify, "cert": cert} def get_adapter(self, url: str) -> BaseAdapter: for prefix, adapter in self.adapters.items(): if url.lower().startswith(prefix.lower()): return adapter raise InvalidSchema(f"No connection adapters were found for {url!r}") def close(self) -> None: for v in self.adapters.values(): v.close() def mount(self, prefix: str, adapter: BaseAdapter) -> None: self.adapters[prefix] = adapter keys_to_move = [k for k in self.adapters if len(k) < len(prefix)] for key in keys_to_move: self.adapters[key] = self.adapters.pop(key) def __getstate__(self) -> dict[str, Any]: state = {attr: getattr(self, attr, None) for attr in self.__attrs__} return state def __setstate__(self, state: dict[str, Any]) -> None: for attr, value in state.items(): setattr(self, attr, value)

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.15303.399−505.012
Source linesThe number of non-empty source lines.435+174
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.323−6.485

Intercept 8.87 + feature contributions = -328.627 (log-odds).
Score = sigmoid(-328.627) = 0.

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.436−14.401
Indentation frequencyHow rapidly indentation changes from line to line.435+14.309
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.435−9.561
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.434.921+5.67
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.0−0.804
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.87.943−0.06
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.096+0.006

Intercept 0.07 + feature contributions = -4.771 (log-odds).
Score = sigmoid(-4.771) = 0.008.

Score0.639
Score165
Run 1

The code is generally well-structured with clear intent, good typing, and reasonable action granularity. The Session class methods are logically organized and each method has a single responsibility. However, there are several readability issues: many empty placeholder comment lines suggest missing documentation, especially in __init__ where comments are blank. The import section has odd trailing whitespace in parentheses. Methods like send() are quite long and have multiple logical sections that could be extracted. Some nested conditions in merge_environment_settings could be flattened. The send() method in particular has complex flow with redirect handling that could be more clearly factored. The mount() method has non-obvious reordering logic. Type hints are good but some are missing import definitions (Self, Unpack, _t, BaseAdapter) which may confuse readers. Overall, the code is understandable but verbose, with opportunities to improve clarity through better documentation and method decomposition.

Run 2

The code is highly readable with clear structure, logical organization, and generally flat nesting. Type hints and docstrings enhance clarity. The `__init__` method is verbose but intentionally so, with comments explaining each attribute. Methods are concise and focused, maintaining a natural reading flow. Minor issues include the `__init__` having many lines and some methods like `send` having moderate complexity, but overall the code is well-structured and easy to follow.

Run 3

This code is highly readable, demonstrating excellent structure and organization. It follows a logical class layout with clear sections for initialization, request preparation, HTTP methods, and settings management, resulting in flat nesting and natural reading flow. Type hints are used consistently, and method purposes are evident from their names and docstrings. Many operations are broken into single-action lines, especially in __init__ and merge_environment_settings, which enhances clarity. Although the file is somewhat long, it remains appropriately concise for the complexity of an HTTP session manager. Minor drawbacks include a few complex expressions in prepare_request (e.g., nested merge_cookies calls) and reliance on sentinel values (None) for overloaded parameters in merge_environment_settings, which slightly increase ambiguity. Overall, the code exemplifies maintainable, professional-grade practices with very few readability compromises.

Run 1

Code Structure: Methods logically separate initialization, request preparation, sending, configuration, and adapter management. Nesting: Mostly shallow, with moderate nesting in proxy merging and redirect handling. Clarity of intent: Descriptive method names and type annotations help, but missing definitions/imports, absent documentation, and terse variables such as p, r, and v obscure details. Code length: Excessive blank lines, a large request signature, and repetitive HTTP wrappers increase visual length, though most logic is purposeful. Action granularity: Generally one action per line, with well-formatted multiline calls; the bare r.content expression hides a side effect. Reading flow: Request preparation and sending follow a natural sequence, but irregular whitespace and undocumented redirect/history manipulation interrupt comprehension.

Run 2

1. Code Structure: Methods logically separate initialization, request preparation, sending, configuration, and cleanup, though several referenced helpers and types are absent from the snippet. 2. Nesting: Mostly shallow; proxy handling and redirect processing introduce moderate, manageable nesting. 3. Clarity of intent: Descriptive method names and annotations communicate purpose, but abbreviated variables, missing documentation, and implicit behavior such as accessing r.content reduce clarity. 4. Code length: Convenience methods are concise, but excessive blank lines, unused imports, and some redundant intermediate variables inflate the code. 5. Action granularity: Statements generally perform one action per line, and multiline calls make arguments easy to scan. 6. Reading flow: The request-to-response sequence is natural, but inconsistent spacing and unexplained cookie, hook, and redirect operations interrupt comprehension.

Run 3

Code Structure: Methods logically separate initialization, request preparation, sending, configuration, and cleanup, although several referenced helpers and types are absent from the snippet. Nesting: Generally shallow, with modest additional nesting in proxy merging and redirect handling. Clarity of intent: Descriptive method names and type annotations communicate purpose, but abbreviated variables such as p and r, missing documentation, and subtle side effects such as r.content reduce clarity. Code length: Excessive blank lines substantially inflate the code; repetitive HTTP convenience methods and lengthy signatures add some bulk but remain understandable. Action granularity: Most statements perform one action, and multiline calls are clearly formatted. Reading flow: The request-to-response sequence is natural, but whitespace noise, unexplained redirect bookkeeping, and unresolved references interrupt smooth reading.