INW000 unsupported-encoding¶
What it does¶
Reports a file in a layer or a context whose PEP 263 declaration (# coding: ... on line 1, or on line 2 after a blank or comment line) names a codec Inwards doesn't read. UTF-8 and its variants, ASCII, Latin-1, ISO-8859-* and cp125x are read as usual, because none of their bytes can turn into a line break or a quote. Any other codec, such as unicode_escape, utf-7, utf-16 or shift_jis, gets one INW000 on line 1, and the file's imports are not checked.
Why is this bad¶
Under unicode_escape, the text #\u000aimport shop.infrastructure.db is a comment to any tool that reads the file as UTF-8, and an import to CPython, which decodes \u000a to a line break first. An agent looking for a way past the layer check, or copying a trick from its training data, can hide any import that way. Inwards reads files as UTF-8 and ships no codec tables, so it reports the file rather than pass it unchecked (ADR-014).
Example¶
With the layers from INW001:
shop/domain/order.py:1:1: INW000 The file declares encoding "unicode-escape", which can hide imports from Inwards, so its imports were not checked.
fix: Save the file as UTF-8 and remove the coding declaration.
1. Re-encode the file as UTF-8 without changing its code.
2. Delete the `# coding: ...` line, or change it to `# coding: utf-8`.
docs: https://sircypkowskyy.github.io/inwards/rules/INW000/
Fixed: the file is UTF-8, and a comment is a comment again.
How to fix¶
- Re-encode the file as UTF-8 without changing its code (
iconv -f SHIFT_JIS -t UTF-8, or your editor's "save with encoding"). - Delete the
# coding: ...line, or change it to# coding: utf-8. - Run the check again: the file's imports are checked now, so an import that the old encoding hid shows up as INW001 or another rule.
Fix safety¶
No automatic fix (autofix: false). Re-encoding has to keep every character the same, which a person should check for files with non-ASCII text.
Configuration¶
Always an error. [tool.inwards.rules] can't list INW000 in ignore or severity (a config error, exit 2), and select doesn't turn it off: a file whose declared encoding can hide imports isn't checked at all, so turning INW000 off would let it pass unchecked (ADR-027). It can't be suppressed inline either, and a file with INW000 has no suppressions read, because under its declared encoding a "comment" may be code (ADR-028).
Known limitations¶
- Legitimate files in codecs such as Shift_JIS or EUC-JP are reported too. That is deliberate: a Shift_JIS lead byte can swallow a backslash.
- Only files whose imports are checked get INW000: files in a layer, and every file once contexts are declared. Any other file with such a declaration isn't reported, though INW007 and INW009 still check it.
- Bytes passed to
execorcompilewith an unreadable declared encoding are reported by INW011, not INW000.
References¶
- ADR-014: Report files whose declared encoding can hide imports
- PEP 263: Defining Python Source Code Encodings