google / google/flatbuffers

[Python] `flatc --python` silently overwrites existing `__init__.py` files

Open
#9,229 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
26.5k
Forks
3.7k
PR merge metrics
No merged PRs in 30d

Description

**flatc version:** 25.12.19 (also older)
**Generator:** `--python`

## What

`flatc --python` creates an empty `__init__.py` in every folder of the
namespace path. If one of those folders already has a hand-written
`__init__.py` with code in it, flatc replaces it with an empty file without warning.

## Why

I wanted the generated files to live under my apps ipc folder which deals with the websocket server and the flatbuffer stuff

## Repro

Project already has these real files:

```
src/myapp/__init__.py # e.g. `def main(): ...`
src/myapp/ipc/__init__.py # e.g. re-exports
```

`schema/foo.fbs`:

```fbs
namespace myapp.ipc.generated;
table Thing { x: int; }
```

Run:

```
flatc --python -o src schema/foo.fbs
```

Now `src/myapp/__init__.py` and `src/myapp/ipc/__init__.py` are empty.
I found it happened through basedpyright lsp:
`ImportError: cannot import name 'main' from 'myapp'`.

## Expected

flatc should not destroy source files it did not generate. Any of:

- don't touch an `__init__.py` in a folder that already exists
- never overwrite a non-empty `__init__.py`
- only write `__init__.py` for the innermost namespace folder
- add a flag like `--python-no-init`

## Notes

- `--gen-onefile` skips the `__init__.py` chain but doesn't emit
cross-schema imports, so it breaks for schemas that use `include`. Not a
workaround.
- `--ts` is fine — it writes into a fresh tree and doesn't clobber
hand-written files.
- `flatc --help` (25.12.19) has no flag to control `__init__.py` output.

## Workarounds

1. Generate into a temp dir, move only the leaf namespace folder into place.
2. Give the generated code its own top-level package, with a namespace
prefix no hand-written module shares.

Contributor guide

Open the contributing guide

Research direction

Start with the flatc --python entry point and reproduce the issue using schema/foo.fbs and the command shown. Trace how the namespace __init__.py files are written, then add coverage showing existing non-empty files are preserved and run the relevant flatc tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.