Coder Social home page Coder Social logo

python-formate / flake8-encodings Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 2.0 259 KB

A Flake8 plugin to identify incorrect use of encodings.

Home Page: https://flake8-encodings.readthedocs.io/en/latest

License: MIT License

Python 95.97% CSS 1.35% HTML 1.00% Just 1.68%
flake8 python encodings pep597 unicode

flake8-encodings's People

Contributors

domdfcoding avatar pre-commit-ci[bot] avatar repo-helper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

flake8-encodings's Issues

KeyError from get_inferred_types when validating simple script

Description

I'm getting a traceback (ending in the line KeyError: '4305da1ea25c27fce08bd14001b76fd54fe42a0724bbd5168c76680a56eda5be') when running flake8 on a simple test script. It appears to have to do with a bad column number passed to jedi in get_inferred_types.

Here's the test script, saved on my machine as test.py:

value = 35.0
something = str(
    value).replace(' ', ',').split(',')

Steps to Reproduce

To reproduce:

  • Save the above script in your current directory under the name test.py
  • Create a new Python 3.10 venv and activate it
  • Do a pip install flake8-encodings jedi
  • Run python -m flake8 test.py

Example session showing the above:

mdickinson@mirzakhani Desktop % python --version
Python 3.10.1
mdickinson@mirzakhani Desktop % python -m venv --clear ~/.venvs/flake8-encodings
mdickinson@mirzakhani Desktop % source ~/.venvs/flake8-encodings/bin/activate
(flake8-encodings) mdickinson@mirzakhani Desktop % pip install flake8-encodings jedi
Collecting flake8-encodings
  Using cached flake8_encodings-0.4.0-py3-none-any.whl (25 kB)
Collecting jedi
  Using cached jedi-0.18.1-py2.py3-none-any.whl (1.6 MB)
Collecting flake8>=3.7
  Using cached flake8-4.0.1-py2.py3-none-any.whl (64 kB)
Collecting astatine>=0.3.1
  Using cached astatine-0.3.1-py3-none-any.whl (6.8 kB)
Collecting flake8-helper>=0.1.1
  Using cached flake8_helper-0.2.0-py3-none-any.whl (13 kB)
Collecting domdf-python-tools>=2.8.1
  Using cached domdf_python_tools-3.1.0-py3-none-any.whl (120 kB)
Collecting parso<0.9.0,>=0.8.0
  Using cached parso-0.8.3-py2.py3-none-any.whl (100 kB)
Collecting asttokens>=1.1
  Using cached asttokens-2.0.5-py2.py3-none-any.whl (20 kB)
Collecting six
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting typing-extensions>=3.7.4.1
  Using cached typing_extensions-4.0.1-py3-none-any.whl (22 kB)
Collecting natsort>=7.0.1
  Using cached natsort-8.0.2-py3-none-any.whl (37 kB)
Collecting pycodestyle<2.9.0,>=2.8.0
  Using cached pycodestyle-2.8.0-py2.py3-none-any.whl (42 kB)
Collecting pyflakes<2.5.0,>=2.4.0
  Using cached pyflakes-2.4.0-py2.py3-none-any.whl (69 kB)
Collecting mccabe<0.7.0,>=0.6.0
  Using cached mccabe-0.6.1-py2.py3-none-any.whl (8.6 kB)
Installing collected packages: typing-extensions, six, pyflakes, pycodestyle, natsort, mccabe, flake8, domdf-python-tools, asttokens, parso, flake8-helper, astatine, jedi, flake8-encodings
Successfully installed astatine-0.3.1 asttokens-2.0.5 domdf-python-tools-3.1.0 flake8-4.0.1 flake8-encodings-0.4.0 flake8-helper-0.2.0 jedi-0.18.1 mccabe-0.6.1 natsort-8.0.2 parso-0.8.3 pycodestyle-2.8.0 pyflakes-2.4.0 six-1.16.0 typing-extensions-4.0.1
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/Users/mdickinson/.venvs/flake8-encodings/bin/python -m pip install --upgrade pip' command.
(flake8-encodings) mdickinson@mirzakhani Desktop % python -m flake8 test.py
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/__main__.py", line 4, in <module>
    cli.main()
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/main/cli.py", line 22, in main
    app.run(argv)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/main/application.py", line 375, in run
    self._run(argv)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/main/application.py", line 364, in _run
    self.run_checks()
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/main/application.py", line 271, in run_checks
    self.file_checker_manager.run()
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/checker.py", line 311, in run
    self.run_serial()
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/checker.py", line 295, in run_serial
    checker.run_checks()
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/checker.py", line 597, in run_checks
    self.run_ast_checks()
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8/checker.py", line 500, in run_ast_checks
    for (line_number, offset, text, _) in runner:
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8_encodings/__init__.py", line 362, in run
    class_visitor.first_visit(self._tree, self.filename)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8_encodings/__init__.py", line 213, in first_visit
    self.visit(node)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 418, in generic_visit
    self.visit(item)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 420, in generic_visit
    self.visit(value)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8_encodings/__init__.py", line 319, in visit_Call
    inferred_types = get_inferred_types(self.jedi_script, node)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/flake8_encodings/__init__.py", line 429, in get_inferred_types
    for inferred_name in jedi_script.infer(node.lineno, node.func.col_offset):
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/api/helpers.py", line 487, in wrapper
    return func(self, line, column, *args, **kwargs)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/api/__init__.py", line 247, in infer
    values = helpers.infer(self._inference_state, context, leaf)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/api/helpers.py", line 167, in infer
    return inference_state.infer(context, leaf)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/__init__.py", line 180, in infer
    return helpers.infer_call_of_leaf(context, name)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/helpers.py", line 79, in infer_call_of_leaf
    return context.infer_node(leaf)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/context.py", line 224, in infer_node
    return infer_node(self, node)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/syntax_tree.py", line 156, in infer_node
    return _infer_node_if_inferred(context, element)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/syntax_tree.py", line 169, in _infer_node_if_inferred
    return _infer_node_cached(context, element)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/cache.py", line 44, in wrapper
    rv = function(obj, *args, **kwargs)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/syntax_tree.py", line 174, in _infer_node_cached
    return _infer_node(context, element)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/debug.py", line 81, in wrapper
    return func(*args, **kwargs)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/syntax_tree.py", line 82, in wrapper
    return func(context, *args, **kwargs)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/syntax_tree.py", line 184, in _infer_node
    return infer_atom(context, element)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/syntax_tree.py", line 304, in infer_atom
    return context.py__getattribute__(atom, position=position)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/context.py", line 45, in py__getattribute__
    names = self.goto(name_or_str, position)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/context.py", line 34, in goto
    names = finder.filter_name(filters, name_or_str)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/finder.py", line 35, in filter_name
    for filter in filters:
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/context.py", line 486, in get_global_filters
    yield from context.get_filters(
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/context.py", line 318, in get_filters
    next(filters, None)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/value/module.py", line 63, in get_filters
    ParserTreeFilter(
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/filters.py", line 138, in __init__
    super().__init__(parent_context, node_context)
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/inference/filters.py", line 100, in __init__
    self._parso_cache_node = get_parso_cache_node(
  File "/Users/mdickinson/.venvs/flake8-encodings/lib/python3.10/site-packages/jedi/parser_utils.py", line 287, in get_parso_cache_node
    return parser_cache[grammar._hashed][path]
KeyError: '4305da1ea25c27fce08bd14001b76fd54fe42a0724bbd5168c76680a56eda5be'

Without Jedi in the mix, the file validates correctly.

Actual result:

See above for the traceback.

Expected result:

Successful validation (no output to the console, zero exit code)

Reproduces how often:

100% reproducible.

Version

  • Operating System: macOS 11.6.2
  • Python: 3.10.1
  • flake8-encodings: 0.4.0

Also: jedi 0.18.1, flake8 4.0.1

Installation source

PyPI / pip

ValueError: `column` parameter (xxx) is not in a valid range (yyy-zzz)

Description

When linting a file that contains a specific statement split across multiple lines, flake8-encodings raises a ValueError.

Steps to Reproduce

linting the following file:

subprocess.run(
    "date"
).stdout.strip()

Actual result:

❯ flake8 scripts/repro.py
Traceback (most recent call last):
  File "/home/user/code-personal/capa/.direnv/python-3.11/bin/flake8", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/main/cli.py", line 23, in main
    app.run(argv)
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/main/application.py", line 198, in run
    self._run(argv)
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/main/application.py", line 187, in _run
    self.run_checks()
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/main/application.py", line 103, in run_checks
    self.file_checker_manager.run()
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/checker.py", line 236, in run
    self.run_serial()
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/checker.py", line 214, in run_serial
    self.results = [
                   ^
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/checker.py", line 219, in <listcomp>
    ).run_checks()
      ^^^^^^^^^^^^
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/checker.py", line 524, in run_checks
    self.run_ast_checks()
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8/checker.py", line 426, in run_ast_checks
    for (line_number, offset, text, _) in runner:
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8_encodings/__init__.py", line 363, in run
    class_visitor.first_visit(self._tree, self.filename)
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8_encodings/__init__.py", line 213, in first_visit
    self.visit(node)
  File "/nix/store/ss7212zavvshj2wv7s4sr8np68j3ij9d-devshell-dir/lib/python3.11/ast.py", line 410, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "/nix/store/ss7212zavvshj2wv7s4sr8np68j3ij9d-devshell-dir/lib/python3.11/ast.py", line 418, in generic_visit
    self.visit(item)
  File "/nix/store/ss7212zavvshj2wv7s4sr8np68j3ij9d-devshell-dir/lib/python3.11/ast.py", line 410, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "/nix/store/ss7212zavvshj2wv7s4sr8np68j3ij9d-devshell-dir/lib/python3.11/ast.py", line 420, in generic_visit
    self.visit(value)
  File "/nix/store/ss7212zavvshj2wv7s4sr8np68j3ij9d-devshell-dir/lib/python3.11/ast.py", line 410, in visit
    return visitor(node)
           ^^^^^^^^^^^^^
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8_encodings/__init__.py", line 319, in visit_Call
    inferred_types = get_inferred_types(self.jedi_script, node)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/flake8_encodings/__init__.py", line 433, in get_inferred_types
    for inferred_name in jedi_script.infer(node.lineno, node.func.col_offset + len('.'.join(attr_names[:-1]))):
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/code-personal/capa/.direnv/python-3.11/lib/python3.11/site-packages/jedi/api/helpers.py", line 484, in wrapper
    raise ValueError('`column` parameter (%d) is not in a valid range '
ValueError: `column` parameter (21) is not in a valid range (0-15) for line 1 ('subprocess.run(\n').

Expected result:

no stack trace.

Reproduces how often:

easily reproduced

Version

❯ python --version
Python 3.11.0

❯ pip freeze | grep flake8-encodings
flake8-encodings==0.5.0.post1

Installation source

pip

Other Additional Information:

the following script does not raise an exception, suggesting that perhaps the linter is relying on assumptions about some AST nodes being found on a single line:

subprocess.run("date").stdout.strip()

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.