Skip to content
main
Switch branches/tags
Code

Latest commit

Summary:
**Problem**: There are errors occurring in named tuple for a special case where the named tuple fields contains commas (or multiple spaces).

Ex. G = namedtuple("G", "a, b, c")

**Root cause**: In the preprocessing.ml file: the preprocessor currently splits the field name by single whitespace but this does not account for commas nor repeated whitespace.

**Fix**: I implemented a change to split the field names by either comma or space. This is followed by filtering out all resulting stray empty strings that result from the splitting process. This approach has been tested by multiple different cases and holds up to combinations of commas and whitespace.

**Alternate approaches considered**:

1. Implementing a regex expression to alter the input string.

Reason against approach: it could involve some pretty complicated regex logic: I would have to either check for a single comma followed by 0-infinite whitespace OR no comma followed by 0-infinite whitespace. Other combinations such as multiple commas would be invalid.

2. Implementing a parser that iterates through the string, checks for invalid field name strings and throws errors.

Reason against approach: The python language does NOT throw error at invalid field name string. I didn't want to have different logic implemented by the language and the pyre typechecker.

Thus, I decided to go with the approach of splitting by either comma/space and filtering out empty strings.

Reviewed By: shannonzhu

Differential Revision: D36138262

fbshipit-source-id: b3cac128ae9d6e1ddfa03e09597ce64713758ff7
7ac02b1

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
api
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

lint tests pyre License: MIT

Pyre is a performant type checker for Python compliant with PEP 484. Pyre can analyze codebases with millions of lines of code incrementally – providing instantaneous feedback to developers as they write code. You can try it out on examples in the Pyre Playground.

Pyre ships with Pysa, a security focused static analysis tool we've built on top of Pyre that reasons about data flows in Python applications. Please refer to our documentation to get started with our security analysis.

Requirements

To get started, you need Python 3.6 or later and watchman working on your system. On MacOS you can get everything with homebrew:

$ brew install python3 watchman

On Ubuntu, Mint, or Debian; use apt-get and homebrew:

$ sudo apt-get install python3 python3-pip python3-venv
$ brew install watchman

We tested Pyre on Ubuntu 18.04.5 LTS, CentOS 7, as well as OSX 10.11 and later.

Setting up a Project

We start by creating an empty project directory and setting up a virtual environment:

$ mkdir my_project && cd my_project
$ python3 -m venv ~/.venvs/venv
$ source ~/.venvs/venv/bin/activate
(venv) $ pip install pyre-check

Next, we teach Pyre about our new project:

(venv) $ pyre init

This command will set up a configuration for Pyre (.pyre_configuration) as well as watchman (.watchmanconfig) in your project's directory. Accept the defaults for now – you can change them later if necessary.

Running Pyre

We are now ready to run Pyre:

(venv) $ echo "i: int = 'string'" > test.py
(venv) $ pyre
 ƛ Found 1 type error!
test.py:1:0 Incompatible variable type [9]: i is declared to have type `int` but is used as type `str`.

This first invocation will start a daemon listening for filesystem changes – type checking your project incrementally as you make edits to the code. You will notice that subsequent invocations of pyre will be faster than the first one.

For more detailed documentation, see https://pyre-check.org.

Join the Pyre community

See CONTRIBUTING.md for how to help out.

License

Pyre is licensed under the MIT license.