Sample

pyint-b0f12e90c778-I05

Rewrite eligible decimal integer literals in hexadecimal notation

Matched original source

Lines559

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Dorn0.0010–1; higher is more readable
Mi0.6170–1; higher is more readable
LLOC180Logical lines; fewer is more readable
DeepSeek V4 Pro16.670–20; mean of three runs
GPT-6.1 Sol16.330–20; mean of three runs
BTTF0.4050–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 ( # noqa: F401 DEFAULT_REDIRECT_LIMIT, REDIRECT_STATI, PreparedRequest, Request, Response, ) from .status_codes import codes from .structures import CaseInsensitiveDict from .utils import ( # noqa: F401 DEFAULT_PORTS, default_headers, get_auth_from_url, get_environ_proxies, get_netrc_auth, requote_uri, resolve_proxies, rewind_body, should_bypass_proxies, # re-export for external consumers to_key_val_list, ) class Session(SessionRedirectMixin): """A Requests session. Provides cookie persistence, connection-pooling, and configuration. Basic Usage:: >>> import requests >>> s = requests.Session() >>> s.get('https://httpbin.org/get') <Response [200]> Or as a context manager:: >>> with requests.Session() as s: ... s.get('https://httpbin.org/get') <Response [200]> """ 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: #: A case-insensitive dictionary of headers to be sent on each #: :class:`Request <Request>` sent from this #: :class:`Session <Session>`. self.headers = default_headers() #: Default Authentication tuple or object to attach to #: :class:`Request <Request>`. self.auth = None #: Dictionary mapping protocol or protocol and host to the URL of the proxy #: (e.g. {'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}) to #: be used on each :class:`Request <Request>`. self.proxies = {} #: Event-handling hooks. self.hooks = default_hooks() #: Dictionary of querystring data to attach to each #: :class:`Request <Request>`. The dictionary values may be lists for #: representing multivalued query parameters. self.params = {} #: Stream response content default. self.stream = False #: SSL Verification default. #: Defaults to `True`, requiring requests to verify the TLS certificate at the #: remote end. #: If verify is set to `False`, requests will accept any TLS certificate #: presented by the server, and will ignore hostname mismatches and/or #: expired certificates, which will make your application vulnerable to #: man-in-the-middle (MitM) attacks. #: Only set this to `False` for testing. #: If verify is set to a string, it must be the path to a CA bundle file #: that will be used to verify the TLS certificate. self.verify = True #: SSL client certificate default, if String, path to ssl client #: cert file (.pem). If Tuple, ('cert', 'key') pair. self.cert = None #: Maximum number of redirects allowed. If the request exceeds this #: limit, a :class:`TooManyRedirects` exception is raised. #: This defaults to requests.models.DEFAULT_REDIRECT_LIMIT, which is #: 30. self.max_redirects = DEFAULT_REDIRECT_LIMIT #: Trust environment settings for proxy configuration, default #: authentication and similar. self.trust_env = True #: A CookieJar containing all currently outstanding cookies set on this #: session. By default it is a #: :class:`RequestsCookieJar <requests.cookies.RequestsCookieJar>`, but #: may be any other ``cookielib.CookieJar`` compatible object. self.cookies = cookiejar_from_dict({}) # Default connection adapters. 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: """Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it. The :class:`PreparedRequest` has settings merged from the :class:`Request <Request>` instance and those of the :class:`Session`. :param request: :class:`Request` instance to prepare with this session's settings. :rtype: requests.PreparedRequest """ url = cast("_t.UriType", request.url) method = cast(str, request.method) cookies = request.cookies or {} # Bootstrap CookieJar. if not isinstance(cookies, cookielib.CookieJar): cookies = cookiejar_from_dict(cookies) # Merge with session cookies merged_cookies = merge_cookies( merge_cookies(RequestsCookieJar(), self.cookies), cookies ) # Set environment's basic authentication if not explicitly set. 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: """Constructs a :class:`Request <Request>`, prepares it and sends it. Returns :class:`Response <Response>` object. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'filename': file-like-objects`` for multipart encoding upload. :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) How many seconds to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) <timeouts>` tuple. :type timeout: float or tuple :param allow_redirects: (optional) Set to True by default. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy. :param hooks: (optional) Dictionary mapping hook name to one event or list of events, event must be callable. :param stream: (optional) whether to immediately download the response content. Defaults to ``False``. :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``True``. When set to ``False``, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Setting verify to ``False`` may be useful during local development or testing. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. :rtype: requests.Response """ if isinstance(url, bytes): url = url.decode("utf-8") # Create the Request. 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 the request. 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: r"""Sends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary, list of tuples or bytes to send in the query string for the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.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: r"""Sends a OPTIONS request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response """ kwargs.setdefault("allow_redirects", True) return self.request("OPTIONS", url, **kwargs) def head(self, url: _t.UriType, **kwargs: Unpack[_t.RequestKwargs]) -> Response: r"""Sends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.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: r"""Sends a POST request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.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: r"""Sends a PUT request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response """ return self.request("PUT", url, data=data, **kwargs) def patch( self, url: _t.UriType, data: _t.DataType = None, **kwargs: Unpack[_t.DataKwargs] ) -> Response: r"""Sends a PATCH request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response """ return self.request("PATCH", url, data=data, **kwargs) def delete(self, url: _t.UriType, **kwargs: Unpack[_t.RequestKwargs]) -> Response: r"""Sends a DELETE request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response """ return self.request("DELETE", url, **kwargs) def send(self, request: PreparedRequest, **kwargs: Any) -> Response: """Send a given PreparedRequest. :rtype: requests.Response """ # Set defaults that the hooks can utilize to ensure they always have # the correct parameters to reproduce the previous request. 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) # It's possible that users might accidentally send a Request object. # Guard against that specific failure case. if isinstance(request, Request): raise ValueError("You can only send PreparedRequests.") assert _is_prepared(request) # Set up variables needed for resolve_redirects and dispatching of hooks allow_redirects = kwargs.pop("allow_redirects", True) stream = kwargs.get("stream") hooks = request.hooks # Get the appropriate adapter to use adapter = self.get_adapter(url=request.url) # Start time (approximately) of the request start = preferred_clock() # Send the request r = adapter.send(request, **kwargs) # Total elapsed time of the request (approximately) elapsed = preferred_clock() - start r.elapsed = timedelta(seconds=elapsed) # Response manipulation hooks r = dispatch_hook("response", hooks, r, **kwargs) # Persist cookies if r.history: # If the hooks create history then we want those cookies too for resp in r.history: extract_cookies_to_jar(self.cookies, resp.request, resp.raw) extract_cookies_to_jar(self.cookies, request, r.raw) # Resolve redirects if allowed. if allow_redirects: # Redirect resolving generator. gen = self.resolve_redirects(r, request, **kwargs) history = [resp for resp in gen] else: history = [] # Shuffle things around if there's history. if history: # Insert the first (original) request at the start history.insert(0x0, r) # Get the last request made r = history.pop() r.history = history # If redirects aren't being followed, store the response on the Request for Response.next(). if not allow_redirects: try: r._next = next( # type: ignore[assignment] # yield_requests=True returns PreparedRequest 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]: """ Check the environment and merge it with some settings. :rtype: dict """ # Gather clues from the surrounding environment. if self.trust_env: # Set environment's proxies. 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) # Look for requests environment configuration # and be compatible with cURL. if verify is True or verify is None: verify = ( os.environ.get("REQUESTS_CA_BUNDLE") or os.environ.get("CURL_CA_BUNDLE") or verify ) # Merge all the kwargs. 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: """ Returns the appropriate connection adapter for the given URL. :rtype: requests.adapters.BaseAdapter """ for prefix, adapter in self.adapters.items(): if url.lower().startswith(prefix.lower()): return adapter # Nothing matches :-/ raise InvalidSchema(f"No connection adapters were found for {url!r}") def close(self) -> None: """Closes all adapters and as such the session""" for v in self.adapters.values(): v.close() def mount(self, prefix: str, adapter: BaseAdapter) -> None: """Registers a connection adapter to a prefix. Adapters are sorted in descending order by prefix length. """ 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.15583.684−514.262
Source linesThe number of non-empty source lines.559+223.6
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.535−6.802

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

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.560−18.792
Indentation frequencyHow rapidly indentation changes from line to line.366+11.922
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.366−7.978
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.558.925+7.403
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.105.984+0.906
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.105.941−0.822
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.048+0.48

Intercept 0.07 + feature contributions = -6.811 (log-odds).
Score = sigmoid(-6.811) = 0.001.

Score0.617
Score180
Run 1

The code is well-structured and generally readable, with clear class organization, descriptive names, and extensive docstrings. Functions are cohesive and follow logical flow. However, readability is somewhat reduced by long parameter lists, deep type annotations, and lengthy docstrings that can obscure the method signatures. The `request` and `send` methods are somewhat dense with mixed responsibilities, but still follow a step-by-step narrative. Nesting is mostly shallow, and actions are usually one per line. Some sections, like `send`, combine multiple logical steps and could be split for clarity. Overall, this is maintainable production code with only mild complexity.

Run 2

The code is highly readable, especially considering it's production-grade library code (likely 'requests'). The structure is logical: imports are organized, class attributes are clearly typed, and each method has a single, clear responsibility with thorough docstrings. Nesting is generally flat; conditional blocks are short and easy to follow. Intent is explicit through descriptive names, comments, and type hints. Code length is appropriate—while verbose in places (e.g., extensive docstrings), this enhances clarity rather than bloat. Action granularity is strong: most lines perform one operation, with intuitive grouping. Reading flow is natural, following a clear top-to-bottom narrative within methods. Minor deductions: some methods like 'send' are lengthy and could be decomposed further, a few inline comments are redundant, and the use of '0x0' instead of '0' for list insertion is slightly unclear. Overall, very maintainable and readable.

Run 3

The code is highly readable with clear organization: imports are grouped logically, the class has a well-documented purpose, and methods are consistently ordered with descriptive docstrings. Nesting is generally flat—most methods use sequential statements and only shallow conditional blocks. Intent is explicit through naming, type hints, and comments that explain non-obvious behavior (e.g., cookie merging, environment settings). Code length is appropriate but some methods (e.g., `request`, `send`) are lengthy due to extensive documentation; however, this is acceptable for a public API. Action granularity is mostly one action per line, with occasional multi-argument calls that remain clear. Reading flow is natural, moving from initialization to request preparation to sending, and helper methods like `merge_environment_settings` are well-placed. Minor deductions: some complexity in `send` around redirect handling could be extracted, and a few comments are verbose, slightly slowing flow.

Run 1

Code Structure: Well-organized class with distinct methods for configuration, request preparation, sending, and adapter management; send handles several responsibilities but remains sequential. Nesting: Mostly shallow, with modest nesting in environment merging and redirect handling. Clarity of intent: Descriptive method names, annotations, and extensive documentation clarify behavior; terse variables such as p and r, implicit content loading through r.content, and definitions absent from the snippet reduce clarity. Code length: Implementation is generally concise, but repetitive wrapper documentation, lengthy configuration comments, and some redundant intermediate variables increase scanning effort. Action granularity: Statements generally perform one action each; multiline calls clearly separate arguments, though nested cookie merging combines operations. Reading flow: Initialization followed by request preparation, convenience methods, sending, and lifecycle utilities creates a natural progression. Consistent formatting and explanatory comments help, while the long documentation blocks interrupt the operational flow.

Run 2

Code Structure: Methods are logically grouped around session initialization, request preparation, sending, and adapter management, though send handles several responsibilities. Nesting: Mostly shallow, with moderate nesting in environment merging and redirect handling. Clarity of intent: Descriptive method names, annotations, and documentation explain behavior well; abbreviated variables such as p and r, unexplained dependencies absent from the snippet, and the side-effect-only r.content expression reduce clarity. Code length: Executable logic is reasonably concise, but extensive, repetitive docstrings and comments make scanning slower. Action granularity: Generally one action per statement, with multiline calls clearly separating arguments; nested cookie merging is slightly denser. Reading flow: The prepare-configure-send sequence is natural and formatting is consistent, although long signatures, documentation blocks, and the multi-stage send method interrupt flow.

Run 3

Code Structure: Well-organized class with distinct methods for preparation, sending, configuration, and lifecycle management; send handles several responsibilities. Nesting: Mostly shallow, with moderate nesting in environment merging and redirect handling. Clarity of intent: Descriptive method names, annotations, and documentation explain the API well, but terse variables such as p and r, unexplained dependencies absent from the snippet, and side-effect-only r.content reduce clarity. Code length: Executable logic is reasonably concise, although repeated wrapper docstrings and extensive initialization comments make the class lengthy. Action granularity: Generally one operation per statement, with clean multiline calls; nested cookie merging slightly compresses distinct steps. Reading flow: The prepare-merge-send sequence and consistent formatting are natural to follow. Large signatures, verbose documentation, and redirect bookkeeping occasionally interrupt that flow.