question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fields() from DataClasses not working properly

See original GitHub issue

Hey I just wanted to ask about strange error I have. fileds() from dataclasses not working properly if dataclass is declared outside running function.

Minimalistic example:

DataClass in function

from dataclasses import dataclass, fields

import submitit


def run():
    @dataclass
    class Test:
        test: str = "test"
        u: int = 1

    t = Test()
    print(f"Test: {fields(t)}")
    return 1


executor = submitit.AutoExecutor(folder="log_test")
executor.update_parameters(timeout_min=1, slurm_partition="dev")
job = executor.submit(run)
print(job.result())
print(job.stdout())

Print:

Test: (Field(name='test',type=<class 'str'>,default='test',default_factory=<dataclasses._MISSING_TYPE object at 0x7f5bceda6cd0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD), Field(name='u',type=<class 'int'>,default=1,default_factory=<dataclasses._MISSING_TYPE object at 0x7f5bceda6cd0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD))

DataClass outside function

from dataclasses import dataclass, fields

import submitit

@dataclass
class Test:
    test: str = "test"
    u: int = 1

def run():
    t = Test()
    print(f"Test: {fields(t)}")
    return 1


executor = submitit.AutoExecutor(folder="log_test")
executor.update_parameters(timeout_min=1, slurm_partition="dev")
job = executor.submit(run)
print(job.result())
print(job.stdout())

Print:

Test: ()

The exact same thing happens on slurm cluster with python 3.7.4 and on my local computer without slurm with python 3.9. Is this submitit or threading issue?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jgboscommented, May 18, 2021

Looks like we are all hitting this issue at the same time: https://github.com/facebookresearch/hydra/issues/1621

0reactions
gwenzekcommented, Mar 9, 2021

I think it should work if you move the dataclass to another file and that you import it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Why is dataclass is not working correctly?
Checking sys.path shows that (1) is listed and therefore python should find the module dataclasses. I checked numpy (installed via pip) and it ......
Read more >
dataclasses: ClassVar attributes are not working properly
I was trying to do something like @dataclass class A: x: ClassVar = set() and thanks to you I know it should be...
Read more >
False Error Declaring Dataclass with Field Returned from a ...
PyCharm correctly infers that the return type of metadata is dataclasses.Field . However, because PyCharm cannot prove that default is not set, it...
Read more >
How to use the dataclasses.field function in dataclasses
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
Python dataclass inheritance, finally ! | by Anis Campos
Some of you might be thinking “I don't see the issue here, you just have to reorder you fields and problem solved!”. Well,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found