├── .codeclimate.yml ├── .gitignore ├── .travis.yml ├── CHANGES.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── flake8_coding.py ├── run_tests.py ├── setup.cfg ├── setup.py ├── testsuite ├── 2nd-line.py ├── 3rd-line.py ├── empty.py ├── invalid.py ├── latin1.py ├── nocodings.py ├── utf8.py └── vim-style.py └── tox.ini /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | languages: 2 | Python: true 3 | exclude_paths: 4 | - "testsuite/*.py" 5 | - "run_tests.py" 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.egg-info 2 | *.pyc 3 | 4 | .coverage 5 | .tox/ 6 | bin/ 7 | dist/ 8 | include/ 9 | lib/ 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | sudo: false 3 | dist: trusty 4 | cache: pip 5 | 6 | matrix: 7 | include: 8 | - python: 2.7 9 | env: TOXENV=py27 10 | - python: 3.4 11 | env: TOXENV=py34 12 | - python: 3.5 13 | env: TOXENV=py35 14 | - python: 3.6 15 | env: TOXENV=py36 16 | - python: 3.6 17 | env: TOXENV=py36-check 18 | - python: 2.7 19 | env: TOXENV=coverage 20 | install: pip install tox 21 | script: tox 22 | -------------------------------------------------------------------------------- /CHANGES.rst: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | 1.3.2 (2019-06-16) 5 | ------------------ 6 | * Fix a bug 7 | 8 | - #18 Use first line number for C101 message 9 | 10 | 1.3.1 (2016-09-09) 11 | ------------------ 12 | * Fix bugs 13 | 14 | - #14 Can't read from stdin using flake8-coding and flake8-print at the same time 15 | - #16 flake8-coding fails on python 3.7 with "RuntimeError: generator raised StopIteration" 16 | 17 | 1.3.0 (2016-08-20) 18 | ------------------ 19 | * Drop Python 2.6 support 20 | * Fix a bug 21 | 22 | - Fix no-accept-encodings to work in config files 23 | 24 | 1.2.2 (2016-06-28) 25 | ------------------ 26 | * Fix a bug 27 | 28 | - Could not work under flake8 >= 3.0 29 | 30 | 1.2.1 (2016-05-20) 31 | ------------------ 32 | * Add option `no-accept-encodings`. If set, will warn for files containing a `coding:` magic comment. 33 | * Fix a bug 34 | 35 | - #4 stdin not supported 36 | 37 | 1.2.0 (not released) 38 | -------------------- 39 | 40 | 1.1.1 (2015-10-25) 41 | ------------------ 42 | * Fix a bug 43 | 44 | - Fix #2 ignore errors if target file is not found 45 | 46 | 1.1.0 (2015-06-27) 47 | ------------------ 48 | * Do not warn for empty file (Thanks to gforcada) 49 | 50 | 1.0.1 (2015-03-25) 51 | ------------------ 52 | * Fix a bug 53 | 54 | - Fix typo in option name: accept-encodings 55 | 56 | 1.0.0 (2015-03-21) 57 | ------------------ 58 | * Initial release 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include MANIFEST.in 2 | include README.rst 3 | include LICENSE 4 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Flake8 Coding plugin 2 | ===================== 3 | 4 | .. image:: https://travis-ci.org/tk0miya/flake8-coding.svg?branch=master 5 | :target: https://travis-ci.org/tk0miya/flake8-coding 6 | 7 | .. image:: https://coveralls.io/repos/tk0miya/flake8-coding/badge.svg?branch=master 8 | :target: https://coveralls.io/r/tk0miya/flake8-coding?branch=master 9 | 10 | .. image:: https://codeclimate.com/github/tk0miya/flake8-coding/badges/gpa.svg 11 | :target: https://codeclimate.com/github/tk0miya/flake8-coding 12 | :alt: Code Climate 13 | 14 | 15 | Adds coding magic comment checks (``coding:``) to ``flake8``. 16 | 17 | Install 18 | -------- 19 | 20 | Install with pip:: 21 | 22 | $ pip install flake8-coding 23 | 24 | You can check that ``flake8`` has picked it up by looking for ``flake8_coding`` 25 | in the output of ``--version``: 26 | 27 | .. code-block:: sh 28 | 29 | $ flake8 --version 30 | 2.5.4 (pep8: 1.7.0, pyflakes: 1.0.0, flake8_coding: 1.1.1, mccabe: 0.4.0) CPython 2.7.11 on Darwin 31 | 32 | Options 33 | ------- 34 | 35 | ``accept-encodings`` 36 | ~~~~~~~~~~~~~~~~~~~~ 37 | 38 | A comma-separated list of acceptable source code encodings for the ``coding:`` 39 | magic comments in files. Default is ``latin-1, utf-8``. 40 | 41 | You can pass this as a command-line argument to ``flake8``, e.g. 42 | ``--accept-encodings=utf-8,utf-16``, or put it in your config file, e.g.: 43 | 44 | .. code-block:: ini 45 | 46 | [flake8] 47 | accept-encodings = utf-8,utf-16 48 | 49 | ``no-accept-encodings`` 50 | ~~~~~~~~~~~~~~~~~~~~~~~ 51 | 52 | If activated, this disallows all ``coding:`` magic comments, no matter their 53 | encoding. This might be useful for Python 3 projects where UTF-8 is the default 54 | and you don't want other encodings used in your project. 55 | 56 | You can pass this as a command-line argument to ``flake8``, e.g. 57 | ``--no-accept-encodings``, or put it in your config file, e.g.: 58 | 59 | .. code-block:: ini 60 | 61 | [flake8] 62 | no-accept-encodings = True 63 | 64 | Rules 65 | ----- 66 | 67 | C101 Coding magic comment not found 68 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 69 | 70 | No magic encoding comment was found in the file. As per 71 | `PEP-263 `_, this must be in the 72 | first two lines of the file. 73 | 74 | C102 Unknown encoding found in coding magic comment 75 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 76 | 77 | The encoding found in the magic encoding comment did not match the 78 | ``accept-encodings`` option. 79 | 80 | C103 Coding magic comment present 81 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 82 | 83 | ``no-accept-encodings`` is set, and a magic encoding comment was found in the 84 | file. 85 | 86 | Requirements 87 | ------------- 88 | 89 | * Python 2.7, 3.4 90 | * flake8 91 | 92 | License 93 | -------- 94 | 95 | Apache License 2.0 96 | -------------------------------------------------------------------------------- /flake8_coding.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import re 4 | 5 | __version__ = '1.3.2' 6 | 7 | 8 | class CodingChecker(object): 9 | name = 'flake8_coding' 10 | version = __version__ 11 | 12 | def __init__(self, tree, filename): 13 | self.filename = filename 14 | 15 | @classmethod 16 | def add_options(cls, parser): 17 | parser.add_option( 18 | '--accept-encodings', default='latin-1, utf-8', action='store', 19 | help="Acceptable source code encodings for `coding:` magic comment" 20 | ) 21 | parser.add_option( 22 | '--no-accept-encodings', action='store_true', parse_from_config=True, 23 | help="Warn for files containing a `coding:` magic comment" 24 | ) 25 | 26 | if hasattr(parser, 'config_options'): # for flake8 < 3.0 27 | parser.config_options.append('accept-encodings') 28 | parser.config_options.append('no-accept-encodings') 29 | 30 | @classmethod 31 | def parse_options(cls, options): 32 | if options.no_accept_encodings: 33 | cls.encodings = None 34 | else: 35 | cls.encodings = [e.strip().lower() for e in options.accept_encodings.split(',')] 36 | 37 | def read_headers(self): 38 | if self.filename in ('stdin', '-', None): 39 | try: 40 | # flake8 >= v3.0 41 | from flake8.engine import pep8 as stdin_utils 42 | except ImportError: 43 | from flake8 import utils as stdin_utils 44 | return stdin_utils.stdin_get_value().splitlines(True)[:2] 45 | else: 46 | try: 47 | import pycodestyle 48 | except ImportError: 49 | import pep8 as pycodestyle 50 | return pycodestyle.readlines(self.filename)[:2] 51 | 52 | def run(self): 53 | try: 54 | # PEP-263 says: a magic comment must be placed into the source 55 | # files either as first or second line in the file 56 | lines = self.read_headers() 57 | if len(lines) == 0: 58 | return 59 | 60 | for lineno, line in enumerate(lines, start=1): 61 | matched = re.search(r'coding[:=]\s*([-\w.]+)', line, re.IGNORECASE) 62 | if matched: 63 | if self.encodings: 64 | if matched.group(1).lower() not in self.encodings: 65 | yield lineno, 0, "C102 Unknown encoding found in coding magic comment", type(self) 66 | else: 67 | yield lineno, 0, "C103 Coding magic comment present", type(self) 68 | break 69 | else: 70 | if self.encodings: 71 | yield 1, 0, "C101 Coding magic comment not found", type(self) 72 | except IOError: 73 | pass 74 | -------------------------------------------------------------------------------- /run_tests.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import unittest 4 | from collections import namedtuple 5 | 6 | from flake8_coding import CodingChecker 7 | 8 | from mock import patch 9 | 10 | 11 | Options = namedtuple('Options', 'no_accept_encodings, accept_encodings') 12 | 13 | 14 | class TestFlake8Coding(unittest.TestCase): 15 | def test_has_utf8_coding_header(self): 16 | checker = CodingChecker(None, 'testsuite/utf8.py') 17 | checker.encodings = ['latin-1', 'utf-8'] 18 | ret = list(checker.run()) 19 | self.assertEqual(ret, []) 20 | 21 | def test_file_not_found(self): 22 | checker = CodingChecker(None, 'file_not_found') 23 | checker.encodings = ['latin-1', 'utf-8'] 24 | ret = list(checker.run()) 25 | self.assertEqual(ret, []) 26 | 27 | def test_empty_file(self): 28 | checker = CodingChecker(None, 'testsuite/empty.py') 29 | checker.encodings = ['latin-1', 'utf-8'] 30 | ret = list(checker.run()) 31 | self.assertEqual(ret, []) 32 | 33 | def test_has_latin1_coding_header(self): 34 | checker = CodingChecker(None, 'testsuite/latin1.py') 35 | checker.encodings = ['latin-1', 'utf-8'] 36 | ret = list(checker.run()) 37 | self.assertEqual(ret, []) 38 | 39 | def test_has_coding_header_at_2nd_line(self): 40 | checker = CodingChecker(None, 'testsuite/2nd-line.py') 41 | checker.encodings = ['latin-1', 'utf-8'] 42 | ret = list(checker.run()) 43 | self.assertEqual(ret, []) 44 | 45 | def test_has_coding_header_at_3rd_line(self): 46 | checker = CodingChecker(None, 'testsuite/3rd-line.py') 47 | checker.encodings = ['latin-1', 'utf-8'] 48 | ret = list(checker.run()) 49 | self.assertEqual(len(ret), 1) 50 | self.assertEqual(ret[0][0], 1) 51 | self.assertEqual(ret[0][1], 0) 52 | self.assertTrue(ret[0][2].startswith('C101 ')) 53 | 54 | def test_has_vim_styled_coding_header(self): 55 | checker = CodingChecker(None, 'testsuite/vim-style.py') 56 | checker.encodings = ['latin-1', 'utf-8'] 57 | ret = list(checker.run()) 58 | self.assertEqual(ret, []) 59 | 60 | def test_has_coding_header_with_invalid_encoding_name(self): 61 | checker = CodingChecker(None, 'testsuite/invalid.py') 62 | checker.encodings = ['latin-1', 'utf-8'] 63 | ret = list(checker.run()) 64 | self.assertEqual(len(ret), 1) 65 | self.assertEqual(ret[0][0], 2) 66 | self.assertEqual(ret[0][1], 0) 67 | self.assertTrue(ret[0][2].startswith('C102 ')) 68 | 69 | def test_has_no_coding_headers(self): 70 | checker = CodingChecker(None, 'testsuite/nocodings.py') 71 | checker.encodings = ['latin-1', 'utf-8'] 72 | ret = list(checker.run()) 73 | self.assertEqual(len(ret), 1) 74 | self.assertEqual(ret[0][0], 1) 75 | self.assertEqual(ret[0][1], 0) 76 | self.assertTrue(ret[0][2].startswith('C101 ')) 77 | 78 | def test_default_encoding(self): 79 | try: 80 | options = Options(False, 'latin-1, utf-8') 81 | CodingChecker.parse_options(options) 82 | self.assertEqual(CodingChecker.encodings, ['latin-1', 'utf-8']) 83 | finally: 84 | if hasattr(CodingChecker, 'encodings'): 85 | del CodingChecker.encodings 86 | 87 | def test_change_encoding(self): 88 | try: 89 | options = Options(False, 'utf-8,utf-16') 90 | CodingChecker.parse_options(options) 91 | self.assertEqual(CodingChecker.encodings, ['utf-8', 'utf-16']) 92 | finally: 93 | if hasattr(CodingChecker, 'encodings'): 94 | del CodingChecker.encodings 95 | 96 | def test_stdin(self): 97 | try: 98 | from flake8.engine import pep8 as stdin_utils # noqa 99 | target = 'flake8.engine.pep8.stdin_get_value' 100 | except ImportError: 101 | from flake8 import utils as stdin_utils # noqa 102 | target = 'flake8.utils.stdin_get_value' 103 | 104 | with patch(target) as stdin_get_value: 105 | with open('testsuite/nocodings.py') as fp: 106 | stdin_get_value.return_value = fp.read() 107 | 108 | for input in ['stdin', '-', None]: 109 | checker = CodingChecker(None, input) 110 | checker.encodings = ['latin-1', 'utf-8'] 111 | ret = list(checker.run()) 112 | self.assertEqual(len(ret), 1) 113 | self.assertEqual(ret[0][0], 1) 114 | self.assertEqual(ret[0][1], 0) 115 | self.assertTrue(ret[0][2].startswith('C101 ')) 116 | 117 | def test_no_accept_encodings_sets_encodings_none(self): 118 | try: 119 | options = Options(True, 'latin-1,utf-8') 120 | CodingChecker.parse_options(options) 121 | self.assertTrue(CodingChecker.encodings is None) 122 | finally: 123 | if hasattr(CodingChecker, 'encodings'): 124 | del CodingChecker.encodings 125 | 126 | def test_encoding_none_with_no_coding_comment(self): 127 | checker = CodingChecker(None, 'testsuite/nocoding.py') 128 | checker.encodings = None 129 | ret = list(checker.run()) 130 | self.assertEqual(ret, []) 131 | 132 | def test_encoding_none_with_coding_comment(self): 133 | checker = CodingChecker(None, 'testsuite/utf8.py') 134 | checker.encodings = None 135 | ret = list(checker.run()) 136 | self.assertEqual(len(ret), 1) 137 | self.assertEqual(ret[0][0], 1) 138 | self.assertEqual(ret[0][1], 0) 139 | self.assertTrue(ret[0][2].startswith('C103 ')) 140 | 141 | 142 | if __name__ == '__main__': 143 | unittest.main() 144 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [build] 2 | build-base = _build 3 | 4 | [sdist] 5 | formats = gztar 6 | 7 | [wheel] 8 | universal = 1 9 | 10 | [aliases] 11 | release = check -r -s register sdist bdist_wheel 12 | 13 | [flake8] 14 | ignore=_ 15 | max-line-length = 120 16 | exclude = .git,.tox,testsuite 17 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import re 3 | 4 | from setuptools import setup 5 | 6 | 7 | def get_version(filename): 8 | """ 9 | Return package version as listed in `__version__` in `filename`. 10 | """ 11 | init_py = open(filename).read() 12 | return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1) 13 | 14 | 15 | version = get_version('flake8_coding.py') 16 | 17 | 18 | with open('README.rst') as readme_file: 19 | readme = readme_file.read() 20 | 21 | 22 | setup( 23 | name='flake8-coding', 24 | version=version, 25 | description='Adds coding magic comment checks to flake8', 26 | long_description=readme, 27 | classifiers=[ 28 | 'Development Status :: 3 - Alpha', 29 | 'Programming Language :: Python', 30 | 'Programming Language :: Python :: 2', 31 | 'Programming Language :: Python :: 2.7', 32 | 'Programming Language :: Python :: 3', 33 | 'Programming Language :: Python :: 3.3', 34 | 'Programming Language :: Python :: 3.4', 35 | 'Topic :: Software Development', 36 | ], 37 | author='Takeshi KOMIYA', 38 | author_email='i.tkomiya@gmail.com', 39 | url='https://github.com/tk0miya/flake8-coding', 40 | license='Apache License 2.0', 41 | keywords='pep8 flake8 coding', 42 | py_modules=['flake8_coding'], 43 | install_requires=[ 44 | 'flake8', 45 | ], 46 | entry_points={ 47 | 'flake8.extension': ['C10 = flake8_coding:CodingChecker'], 48 | }, 49 | ) 50 | -------------------------------------------------------------------------------- /testsuite/2nd-line.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | -------------------------------------------------------------------------------- /testsuite/3rd-line.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # 3 | # -*- coding: utf-8 -*- 4 | -------------------------------------------------------------------------------- /testsuite/empty.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tk0miya/flake8-coding/535c20ce0fccd62fc8fae628718749953675286c/testsuite/empty.py -------------------------------------------------------------------------------- /testsuite/invalid.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf -*- 3 | -------------------------------------------------------------------------------- /testsuite/latin1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: latin-1 -*- 2 | -------------------------------------------------------------------------------- /testsuite/nocodings.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # 3 | -------------------------------------------------------------------------------- /testsuite/utf8.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | -------------------------------------------------------------------------------- /testsuite/vim-style.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # vim: set fileencoding=utf-8 : 3 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist=py27,py34,py34-check 3 | 4 | [testenv] 5 | passenv= 6 | TRAVIS* 7 | deps= 8 | mock 9 | commands= 10 | python run_tests.py 11 | flake8 12 | 13 | [testenv:check] 14 | deps = 15 | docutils 16 | Pygments 17 | commands = python setup.py check -s --restructuredtext --metadata 18 | 19 | [testenv:coverage] 20 | deps= 21 | pytest 22 | pytest-cov 23 | coveralls 24 | mock 25 | commands= 26 | py.test run_tests.py --cov flake8_coding --cov-report term-missing 27 | coveralls 28 | --------------------------------------------------------------------------------