PyCharm 2017.2 Help

Flow

This feature is supported in the Professional edition only.

Introduction

PyCharm provides basic support of Flow static type checker that brings type annotations to JavaScript. This support involves recognition and syntax highlighting of Flow structures on all operating systems. If you are using Unix or macOS, you can also run Flow in the command-line mode.

Before you start

  1. Install and enable the NodeJS repository plugin as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.
  2. Download and install the Node.js runtime environment.
  3. Configure the Node.js interpreter in PyCharm:
    1. Open the Settings / Preferences Dialog by choosing File | Settings for Windows and Linux or PyCharm | Preferences for macOS, and click Node.js and NPM under Languages & Frameworks.
    2. On the Node.js and NPM page that opens, specify the location of the desired Node.js interpreter.
    See Configuring Node.js Interpreters for details.

Installing Flow

The easiest way to install the Flow type checker is to use the Node Package Manager (npm), which is a part of Node.js. See Installing and Removing External Software Using Node Package Manager for details.

Depending on the desired location of the Flow type checker executable file, choose one of the following methods:

  • Install the type checker globally at the PyCharm level so it can be used in any PyCharm project.
  • Install the type checker in a specific project and thus restrict its use to this project.
  • Install the type checker in a project as a development dependency.

In either installation mode, make sure that the parent folder of the Flow type checker is added to the PATH variable. This enables you to launch the type checker from any folder.

PyCharm provides user interface both for global and project installation as well as supports installation through the command line.

Installing Flow globally

Global installation makes a type checker available at the PyCharm level so it can be used in any PyCharm project. Moreover, during installation the parent folder of the type checker is automatically added to the PATH variable, which enables you to launch the type checker from any folder.

  • Run the installation from the command line in the global mode:
    1. Open the embedded Terminal (View | Tool Windows | Terminal) and switch to the directory where NPM is stored or define a PATH variable for it so it is available from any folder, see Installing NodeJs.
    2. Type the following command at the command prompt:
      npm install -g flow-bin

      The -g key makes the type checker run in the global mode. Because the installation is performed through NPM, the Flow type checker is installed in the npm folder. Make sure this parent folder is added to the PATH variable. This enables you to launch the type checker from any folder.

      For more details on the NPM operation modes, see npm documentation. For more information about installing the Flow type checker, see https://npmjs.org/package/flow-bin.

  • Run NPM from PyCharm using the Node.js and NPM page of the Settings dialog box.
    1. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PyCharm | Preferences for macOS, and click Node.js and NPM under Languages & Frameworks.
    2. On the Node.js and NPM page that opens, the Packages area shows all the Node.js-dependent packages that are currently installed on your computer, both at the global and at the project level. Click /help/img/idea/2017.2/new.png.
    3. In the Available Packages dialog box that opens, select the required package to install.
    4. Select the Options checkbox and type -g in the text box next to it.
    5. Optionally specify the product version and click Install Package to start installation.

Installing Flow in a project

Local installation in a specific project restricts the use of a type checker to this project.

  • Run the installation from the command line:
    1. Open the embedded Terminal (View | Tool Windows | Terminal) and switch to the project root folder.
    2. At the command prompt, type npm install flow-bin.
  • Run NPM from PyCharm using the Node.js and NPM page of the Settings dialog box.
    1. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PyCharm | Preferences for macOS, and click Node.js and NPM under Languages & Frameworks.
    2. On the Node.js and NPM page that opens, the Packages area shows all the Node.js-dependent packages that are currently installed on your computer, both at the global and at the project level. Click /help/img/idea/2017.2/new.png.
    3. In the Available Packages dialog box that opens, select the required package.
    4. Optionally specify the product version and click Install Package to start installation.

Project level installation is helpful and reliable in template-based projects of the type Node Boilerplate or Node.js Express, which already have the node_modules folder. The latter is important because NPM installs the Flow type checker in a node_modules folder. If your project already contains such folder, the Flow type checker is installed there.

Projects of other types or empty projects may not have a node_modules folder. In this case npm goes upwards in the folder tree and installs the Flow type checker in the first detected node_modules folder. Keep in mind that this detected node_modules folder may be outside your current project root.

Finally, if no node_modules folder is detected in the folder tree either, the folder is created right under the current project root and the Flow type checker is installed there.

In either case, make sure that the parent folder of the Flow type checker is added to the PATH variable. This enables you to launch the type checker from any folder.

For more details, see Getting Started with Flow.

Configuring Flow in PyCharm

To have PyCharm recognize Flow structures, provide correct syntax highlighting, report errors properly, and avoid false-positive error highlighting, change the JavaScript language level to Flow, add a .flowconfig configuration file to your project, and supply every file to be checked with a // @flow comment on top.

  1. To change the language level to Flow:
    1. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or PyCharm | Preferences for macOS, and click JavaScript under Languages & Frameworks.
    2. On the JavaScript page that opens, choose Flow from the JavaScript Language Version drop-down list.
    3. In the Flow executable field, specify the path to the Flow executable file.
    4. In the Use Flow server for: area, specify the basis for coding assistance by selecting or clearing the following checkboxes:
      • Type checking: When this checkbox is selected, syntax and error highlighting is provided based on the data received from the Flow server. When the checkbox is cleared, only the basic internal PyCharm highlighting is available.
      • Navigation, code completion, and type hinting: When this checkbox is selected, suggestion lists for reference resolution and code completiong contain both suggestions retrieved from integration with Flow and suggestions calculated by PyCharm. When the checkbox is cleared, references are resolved through PyCharm calculation only.
      • Code highlighting and built-in inspections: Select this checkbox to power native Flow code highlighting and built-in inspections. Please note that enabling this option may cause performance problems. By default, the checkbox is cleared.

      The checkboxes are available only when the path to the Flow executable file is specified.

  2. To have a .flowconfig configuration file generated in your project: open the embedded Terminal (View | Tool Windows | Terminal) and run the flow init command at the command prompt.
  3. To have a file checked with the Flow checker, add a // @flow comment at the top of it: just type flow and press Tab and PyCharm will expand it into // @flow.
Last modified: 26 October 2017

See Also