Skip to main content

A flake8 plugin to use consistent escaping style in string or bytes literals

Project description

flake8-escaping-style

A flake8 plugin to help maintain a consistent style of escape sequences in string and bytes literals throughout your project.

Installation

This plugin requires at least version 3.7 of Python.

Like most flake8 plugins, start by installing the package:

pip install flake8-escaping-style

Then in your flake8 configuration, use a combination of select and ignore that matches your preferences.

For example, here's how it would look like if you wanted to prevent all escapes except for \N{name of character}:

select=ESC1
ignore=ESC105

List of error codes

For string literals (str)

Error code Escape sequence style Example string ("café")
ESC101 Octal (3 digits) "caf\351"
ESC102 Hexadecimal (2 digits) "caf\xe9"
ESC103 Hexadecimal (4 digits) "caf\u00e9"
ESC104 Hexadecimal (8 digits) "caf\U000000e9"
ESC105 Named character "caf\N{latin small letter e with acute}"

For byte string literals (bytes)

For completeness's sake, this plugin can also detect escape styles in bytestring literals:

Error code Escape sequence style Example bytestring
ESC201 Octal (3 digits) b"caf\351"
ESC202 Hexadecimal (2 digits) b"caf\xE9"

Motivation

Python has several different ways to write escape sequences.

In the old days of Python 2, any file was assumed to be ascii encoded unless you added a special comment on the first line. That meant that if you wanted to write the string "café" in your source code, you had to either add that comment on the first line of your file, or use an escape sequence like "caf\xe9" or "caf\u00E9".

Nowadays, Python has lifted those limitations and uses a more practical default encoding (utf-8, see PEP3120). So for most string literals you can use the character you want without having to use escape sequences at all.

I'd still recommend using an escape sequence when using "weird" [^1] characters and especially invisible ones. But in that case I like to use the "named character" escaping style \N{name}.

[^1]: By "weird" I mean any character that requires more than two fingers to type on my keyboard.

Consider for example the case of a non-breaking space. It's a nifty little character and it can be very useful sometimes but it can also lead to some tough debugging:

Can you spot the difference?

>>> "hello world" == "hello world"
False

Using an escape sequence here (rather than the character itself) makes it more obvious as to what is going on:

>>> "hello world" == "hello\xa0world"
False

That's better in my opinion, but it still requires knowing which character has the number A0 in hexadecimal. The best option is to use a named escape sequence:

>>> "hello world" == "hello\N{no-break space}world"
False

For that reason, I prefer my projects to only use the "named" style of escape sequences (ESC105).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-escaping-style-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

flake8_escaping_style-0.1.0-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flake8-escaping-style-0.1.0.tar.gz.

File metadata

  • Download URL: flake8-escaping-style-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for flake8-escaping-style-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e0c5fbc2682d50f440a06d61ba1d8f18a6ac0c533a0f22240811e53660596c21
MD5 f4c79843d61f4a52093496d177e8488f
BLAKE2b-256 de98607b0e58c412644febdd63084304e2f4515cbd43861c96c2ca567ac39691

See more details on using hashes here.

File details

Details for the file flake8_escaping_style-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: flake8_escaping_style-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for flake8_escaping_style-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 48536fc28b01fd89b6a9b648577667bf9379df372d40f147bbb610c39e7e9b4a
MD5 4378b2f822bedc8359539cb1b7ce5f10
BLAKE2b-256 e26a0c7db82fea83bf0e2c77cd791ea6242c0638fb037ec29a087406fbd4f488

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page