Giter Site home page Giter Site logo

Comments (7)

gotgenes avatar gotgenes commented on May 12, 2024

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.

DRMacIver avatar DRMacIver commented on May 12, 2024

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.

gotgenes avatar gotgenes commented on May 12, 2024

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.

DRMacIver avatar DRMacIver commented on May 12, 2024

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.

gotgenes avatar gotgenes commented on May 12, 2024

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.

asottile avatar asottile commented on May 12, 2024

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.

bwendling avatar bwendling commented on May 12, 2024

I think that this is taken care of by: 1d42a26

from yapf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.