• Stars
    star
    285
  • Rank 140,220 (Top 3 %)
  • Language
    Python
  • License
    GNU Lesser Genera...
  • Created about 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

IDE allow you to refactor code, Baron allows you to write refactoring code.

Introduction

Baron is a Full Syntax Tree (FST) library for Python. By opposition to an AST which drops some syntax information in the process of its creation (like empty lines, comments, formatting), a FST keeps everything and guarantees the operation fst_to_code(code_to_fst(source_code)) == source_code.

Roadmap

Current roadmap is as boring as needed:

  • bug fixs
  • new small features (walker pattern, maybe code generation) and performance improvement.

Installation

pip install baron

Basic Usage

from baron import parse, dumps

fst = parse(source_code_string)
source_code_string == dumps(fst)

Unless you want to do low level things, use RedBaron instead of using Baron directly. Think of Baron as the "bytecode of python source code" and RedBaron as some sort of usable layer on top of it.

If you don't know what Baron is or don't understand yet why it might be useful for you, read the « Why is this important? » section.

Documentation

Baron documentation is available on Read The Docs.

Contributing

If you want to implement new grammar elements for newer python versions, here are the documented steps for that: https://github.com/PyCQA/baron/blob/master/add_new_grammar.md

Also note that reviewing most grammar modifications takes several hours of advanced focusing (we can't really afford bugs here) so don't despair if you PR seems to be hanging around, sorry for that :/

And thanks in advance for your work!

Financial support

Baron and RedBaron are a very advanced piece of engineering that requires a lot of time of concentration to work on. Until the end of 2018, the development has been a full volunteer work mostly done by Bram, but now, to reach the next level and bring those projects to the stability and quality you expect, we need your support.

You can join our contributors and sponsors on our transparent OpenCollective, every contribution will count and will be mainly used to work on the projects stability and quality but also on continuing, on the side, the R&D side of those projects.

Our supporters

badge with number of supporters at tier I like this, keep going! badge with number of supporters at tier it looks cool! badge with number of supporters at tier Oh god, that saved me so much time!

Why is this important?

The usage of a FST might not be obvious at first sight so let's consider a series of problems to illustrate it. Let's say that you want to write a program that will:

  • rename a variable in a source file... without clashing with things that are not a variable (example: stuff inside a string)
  • inline a function/method
  • extract a function/method from a series of line of code
  • split a class into several classes
  • split a file into several modules
  • convert your whole code base from one ORM to another
  • do custom refactoring operation not implemented by IDE/rope
  • implement the class browser of smalltalk for python (the whole one where you can edit the code of the methods, not just showing code)

It is very likely that you will end up with the awkward feeling of writing clumpsy weak code that is very likely to break because you didn't thought about all the annoying special cases and the formatting keeps bothering you. You may end up playing with ast.py until you realize that it removes too much information to be suitable for those situations. You will probably ditch this task as simply too complicated and really not worth the effort. You are missing a good abstraction that will take care of all of the code structure and formatting for you so you can concentrate on your task.

The FST tries to be this abstraction. With it you can now work on a tree which represents your code with its formatting. Moreover, since it is the exact representation of your code, modifying it and converting it back to a string will give you back your code only modified where you have modified the tree.

Said in another way, what I'm trying to achieve with Baron is a paradigm change in which writing code that will modify code is now a realist task that is worth the price (I'm not saying a simple task, but a realistic one: it's still a complex task).

Other

Having a FST (or at least a good abstraction build on it) also makes it easier to do code generation and code analysis while those two operations are already quite feasible (using ast.py and a templating engine for example).

Some technical details

Baron produces a FST in the form of JSON (and by JSON I mean Python lists and dicts that can be dumped into JSON) for maximum interoperability.

Baron FST is quite similar to Python AST with some modifications to be more intuitive to humans, since Python AST has been made for CPython interpreter.

Since playing directly with JSON is a bit raw I'm going to build an abstraction on top of it that will looks like BeautifulSoup/jQuery.

State of the project

Currently, Baron has been tested on the top 100 projects and the FST converts back exactly into the original source code. So, it can be considered quite stable, but it is far away from having been battle tested.

Since the project is very young and no one is already using it except my project, I'm open to changes of the FST nodes but I will quickly become conservative once it gets some adoption and will probably accept to modify it only once or twice in the future with clear indications on how to migrate.

Baron is supporting python 2 grammar and up to python 3.7 grammar.

Tests

Run either py.test tests/ or nosetests in the baron directory.

Community

You can reach us on irc.freenode.net#baron or irc.freenode.net##python-code-quality.

Code of Conduct

As a member of PyCQA, Baron follows its Code of Conduct.

Misc

Old blog post announcing the project. Not that much up to date.

More Repositories

1

isort

A Python utility / library to sort imports.
Python
6,308
star
2

bandit

Bandit is a tool designed to find common security issues in Python code.
Python
5,900
star
3

pycodestyle

Simple Python style checker in one Python file
Python
4,924
star
4

pylint

It's not just a linter that annoys you!
Python
4,246
star
5

flake8

flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.
Python
3,068
star
6

pyflakes

A simple program which checks Python source files for errors
Python
1,304
star
7

pydocstyle

docstring style checker
Python
1,105
star
8

flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
Python
1,034
star
9

autoflake

Removes unused imports and unused variables as reported by pyflakes
Python
844
star
10

redbaron

Bottom-up approach to refactoring in python
Python
683
star
11

mccabe

McCabe complexity checker for Python
Python
602
star
12

pylint-django

Pylint plugin for improving code analysis for when using Django
Python
556
star
13

docformatter

Formats docstrings to follow PEP 257
Python
511
star
14

pep8-naming

Naming Convention checker for Python
Python
471
star
15

astroid

A common base representation of python source code for pylint and other projects
Python
425
star
16

modernize

Modernizes Python code for eventual Python 3 migration. Built on top of fissix (a fork of lib2to3)
Python
326
star
17

flake8-import-order

Flake8 plugin that checks import order against various Python Style Guides
Python
276
star
18

eradicate

Removes commented-out code from Python files
Python
195
star
19

doc8

Style checker for sphinx (or other) rst documentation.
Python
158
star
20

flake8-docstrings

Integration of pydocstyle and flake8 for combined linting and reporting
Python
144
star
21

flake8-commas

Flake8 extension for enforcing trailing commas in python
Python
128
star
22

flake8-pyi

A plugin for Flake8 that provides specializations for type hinting stub files
Python
70
star
23

pylint-celery

Pylint plugin for analysing code using Celery
Python
34
star
24

meta

Documentation about how the PyCQA organization works
Python
24
star
25

pylint-plugin-utils

Utilities and helpers for writing Pylint plugins
Python
20
star
26

oeuvre

A repository to collect examples of Python code for testing code-quality tools
Python
11
star
27

flake8-polyfill

Project to make writing plugins across major versions of flake8 easier
Python
11
star
28

flake8-json

JSON formatter for Flake8 output
Python
9
star
29

bandit-action

GitHub Action to run Bandit
Dockerfile
8
star
30

infrastructure

Mirror of PyCQA's infrastructure playbooks
6
star
31

mccabe-console-script

Add a console script for the mccabe complexity checker
Python
4
star