MCPcopy Index your code
hub / github.com/python/mypy / find_python_encoding

Function find_python_encoding

mypy/util.py:125–137  ·  view source on GitHub ↗

PEP-263 for detecting Python file encoding

(text: bytes)

Source from the content-addressed store, hash-verified

123
124
125def find_python_encoding(text: bytes) -> tuple[str, int]:
126 """PEP-263 for detecting Python file encoding"""
127 result = ENCODING_RE.match(text)
128 if result:
129 line = 2 if result.group(1) else 1
130 encoding = result.group(3).decode("ascii")
131 # Handle some aliases that Python is happy to accept and that are used in the wild.
132 if encoding.startswith(("iso-latin-1-", "latin-1-")) or encoding == "iso-latin-1":
133 encoding = "latin-1"
134 return encoding, line
135 else:
136 default_encoding = "utf8"
137 return default_encoding, -1
138
139
140def bytes_to_human_readable_repr(b: bytes) -> str:

Callers 1

decode_python_encodingFunction · 0.85

Calls 3

groupMethod · 0.80
decodeMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…