Skip to content

Commit

Permalink
Added proper type hinting for normalize functions (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
hampusnasstrom authored May 14, 2024
1 parent df4ed6f commit 5eeb253
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ Will create the following python file:
# limitations under the License.
#

from structlog.stdlib import (
BoundLogger,
from nomad.datamodel.data import ArchiveSection
from typing import (
TYPE_CHECKING,
)
from nomad.metainfo import (
Package,
Expand All @@ -85,6 +86,13 @@ from nomad.metainfo import (
from nomad.datamodel.data import (
ArchiveSection,
)
if TYPE_CHECKING:
from nomad.datamodel.datamodel import (
EntryArchive,
)
from structlog.stdlib import (
BoundLogger,
)

m_package = Package(name='Example Schema')

Expand Down Expand Up @@ -114,7 +122,7 @@ class Activity(ArchiveSection):
},
)
def normalize(self, archive, logger: BoundLogger) -> None:
def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
'''
The normalizer for the `Activity` class.
Expand All @@ -132,7 +140,7 @@ class Entity(ArchiveSection):
'''
m_def = Section()

def normalize(self, archive, logger: BoundLogger) -> None:
def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
'''
The normalizer for the `Entity` class.
Expand Down
16 changes: 12 additions & 4 deletions example/__init__.py → example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
# limitations under the License.
#

from structlog.stdlib import (
BoundLogger,
from nomad.datamodel.data import ArchiveSection
from typing import (
TYPE_CHECKING,
)
from nomad.metainfo import (
Package,
Expand All @@ -28,6 +29,13 @@
from nomad.datamodel.data import (
ArchiveSection,
)
if TYPE_CHECKING:
from nomad.datamodel.datamodel import (
EntryArchive,
)
from structlog.stdlib import (
BoundLogger,
)

m_package = Package(name='Example Schema')

Expand Down Expand Up @@ -57,7 +65,7 @@ class Activity(ArchiveSection):
},
)

def normalize(self, archive, logger: BoundLogger) -> None:
def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
'''
The normalizer for the `Activity` class.
Expand All @@ -75,7 +83,7 @@ class Entity(ArchiveSection):
'''
m_def = Section()

def normalize(self, archive, logger: BoundLogger) -> None:
def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
'''
The normalizer for the `Entity` class.
Expand Down
2 changes: 1 addition & 1 deletion src/metainfoyaml2py/metainfoyaml2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def yaml2py(yaml_path: str, output_dir: str = '', normalizers: bool = False,
flake8_cleaned_code = autoflake.fix_code(
code, remove_all_unused_imports=True)
cleaned_code = autopep8.fix_code(
flake8_cleaned_code, options={'aggressive': 2})
flake8_cleaned_code, options={'aggressive': 2, 'max_line_length': 90})
# write the code to file
file.write(cleaned_code)

Expand Down
13 changes: 10 additions & 3 deletions src/metainfoyaml2py/resources/standard_file_content.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ header: |
#
imports: |
import numpy as np
from structlog.stdlib import (
BoundLogger,
from typing import (
TYPE_CHECKING,
)
from nomad.metainfo import (
MSection,
Expand All @@ -35,12 +35,19 @@ imports: |
EntryData,
ArchiveSection,
)
if TYPE_CHECKING:
from nomad.datamodel.datamodel import (
EntryArchive,
)
from structlog.stdlib import (
BoundLogger,
)
package_name: |
m_package = Package(name='%s')
footer: |
m_package.__init_metainfo__()
normalizer: |
def normalize(self, archive, logger: BoundLogger) -> None:
def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
'''
The normalizer for the `%s` class.
Expand Down

0 comments on commit 5eeb253

Please sign in to comment.