jacebrowning / jacebrowning/datafiles

Fields declaration order really matters when using multi-variables pattern

Open
#334 2 comments 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
Python
Stars
213
Forks
23
Avg merge
1d 22h
Merged PRs (30d)
5

Description

It looks to me that the order in which the dataclass fields are declared is super important.

Example directory structure:

```
environments
dev
config
servers
server-1
server.yaml
server-2
server.yaml
server-3
server.yaml
```

Datafile definition:

```
@datafile("./environments/{self.environment}/config/servers/{self.name}/server.yaml")
class Server:
name: str
environment: str
```

When running:

```
import os

from manager.models import Cluster

if __name__ == '__main__':

clusters = list(Server.objects.all())
```

It ends up raising:

`FileNotFoundError: [Errno 2] No such file or directory: '/project/configuration/environments/server-1/config/servers/dev/server.yaml'`

This seems to come from when the manager.all method does yield self.get(*values) with values ["server-1", "dev"] which ends up building the path posted above as manager.get iterates on the fields in the order they are declared and sets the values accordingly.

If I declare:

```
@datafile("./environments/{self.environment}/config/servers/{self.name}/server.yaml")
class Server:
environment: str
name: str
```
It works.

Contributor guide

Open the contributing guide

Research direction

Reproduce the example with the Server dataclass and inspect the manager.all and manager.get paths, especially how declared fields are iterated and matched with values. Done means the example builds the server.yaml path with environment and name mapped correctly without requiring a particular field declaration order.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.