Comments (7)
yapf needs to open source files using codecs.open
, and accept an optional explicit file encoding parameter on the command line interface, as well as for any functions exposed in the API that create a file handle. See #19 for an example usage.
from yapf.
I learned about tokenize.open recently. It handles encoding comments in python source files to infer the correct file encoding.
I think it's python 3 only but the source code might be open source in a way you could use.
from yapf.
Guessing encoding based on the header comment in the source file would be convenient. Keep in mind, though, that it's not always accurate. See Fact of Life 5: Sometimes you are told wrong. This is why it's important to allow users to pass it in explicitly.
from yapf.
Right, but the correct way to pass it in explicitly is to fix your source code so it's not lying. :-) yapf already does not correctly reformat syntactically invalid source after all.
from yapf.
I hadn't thought of it that way, but you make a good point. For convenience, if the user provides a file with encoding declared inside the file via a magic comment matching one of the PEP 263 specifications, then yapf should read this comment line and select this encoding for decoding the file.
I still advocate for yapf and its API to accept encoding as an explicit parameter when reading source files, as there are still cases for which it would be useful; for example, when the source file does not contain an explicit encoding magic comment, or when tokenize.open
(and by dependency, tokenize.detect_encoding
) guess incorrectly. Otherwise, yapf will force users to declare a magic comment to any source file they run it through the tool, which is very opinionated (even though I agree with it) and may overstep the scope of this tool. Such opinion seems better relegated to style checkers like pep8 and Pylint.
from yapf.
Does it make sense to maintain the original encoding or to fix it to something reasonable (utf-8)? In py2 it kind of makes sense to maintain the original encoding as bytestrings will be encoded using that encoding. For py3 there's very little reason to specify a source encoding afaict.
Also if we're going by how the interpreter handles non-ascii (non-utf-8 py3) I think it's reasonable to reject files without an explicit encoding if they are not UTF-8).
Also probably relevant to this discussion: https://www.python.org/dev/peps/pep-3120/
Here's how we detect encoding in another project I've worked on (also dealing with 2to3 + automatic refactorings!) https://github.com/bukzor/RefactorLib/blob/master/refactorlib/python/parse.py#L5-L22
I'm interested in working on this issue.
from yapf.
I think that this is taken care of by: 1d42a26
from yapf.
Related Issues (20)
- The NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS format option doesn't work correctly.
- Remove the blank line between docstring and function head
- Improve error reporting when `yapf` encounters unsupported Python syntax
- Set specific options for keywords
- Function call without arguments.
- Yapf not formatting python generic
- dedent_closing_brackets not working for Annotations[]
- yapf is failing `pre-commit` with `tool.yapfignore` configured: `yapf: input filenames did not match any python files`
- [BUG] yapf with split_all_top_level_comma_separated_values set to true fails to split lines with tuple LHS
- [Bug] [Crash][Reproducible] `EOFError: Ran out of input` when import yapf with multiprocess
- assert formatting
- Strange inconsistent behaviour HOT 3
- Is there a each_dict_entry_on_separate_line for lists? HOT 2
- Try to keep list literals in tuples aligned
- yapf-diff inconsistent with yapf on multiline strings with backslashes
- Ternary conditionals within F Strings Raise IndexError
- Split type hints with trailing comma
- yapf adds spaces before backslash with every run
- Split first and only argument into separate line
- The formatter fails in this file
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yapf.