Skip to content

Commit

Permalink
fix formatting and type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tabergma committed Jan 10, 2025
1 parent 06cae46 commit 143a222
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
4 changes: 2 additions & 2 deletions rasa/nlu/classifiers/logistic_regression_classifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from typing import Any, Text, Dict, List, Type, Tuple

import structlog
from scipy.sparse import hstack, vstack, csr_matrix
from sklearn.linear_model import LogisticRegression

Expand Down Expand Up @@ -184,7 +183,8 @@ def load(

if unknown_types:
logger.debug(
f"Untrusted types ({unknown_types}) found when loading {classifier_file}!",
f"Untrusted types ({unknown_types}) found when "
f"loading {classifier_file}!",
)
raise ValueError()

Expand Down
16 changes: 8 additions & 8 deletions rasa/nlu/extractors/crf_entity_extractor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from collections import OrderedDict
from enum import Enum
import logging
import typing
from collections import OrderedDict
from enum import Enum
from typing import Any, Dict, List, Optional, Text, Tuple, Callable, Type

import numpy as np
Expand All @@ -15,13 +15,12 @@
from rasa.engine.recipes.default_recipe import DefaultV1Recipe
from rasa.engine.storage.resource import Resource
from rasa.engine.storage.storage import ModelStorage
from rasa.nlu.constants import TOKENS_NAMES
from rasa.nlu.extractors.extractor import EntityExtractorMixin
from rasa.nlu.test import determine_token_labels
from rasa.nlu.tokenizers.spacy_tokenizer import POS_TAG_KEY
from rasa.nlu.extractors.extractor import EntityExtractorMixin
from rasa.nlu.tokenizers.tokenizer import Token, Tokenizer
from rasa.shared.nlu.training_data.training_data import TrainingData
from rasa.shared.nlu.training_data.message import Message
from rasa.nlu.constants import TOKENS_NAMES
from rasa.shared.constants import DOCS_URL_COMPONENTS
from rasa.shared.nlu.constants import (
TEXT,
ENTITIES,
Expand All @@ -32,7 +31,8 @@
SPLIT_ENTITIES_BY_COMMA,
SPLIT_ENTITIES_BY_COMMA_DEFAULT_VALUE,
)
from rasa.shared.constants import DOCS_URL_COMPONENTS
from rasa.shared.nlu.training_data.message import Message
from rasa.shared.nlu.training_data.training_data import TrainingData
from rasa.utils.tensorflow.constants import BILOU_FLAG, FEATURIZERS

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -116,7 +116,7 @@ class CRFEntityExtractor(GraphComponent, EntityExtractorMixin):

CONFIG_FEATURES = "features"

function_dict: Dict[Text, Callable[[CRFToken], Any]] = { # noqa: RUF012
function_dict: Dict[Text, Callable[[CRFToken], Any]] = {
CRFEntityExtractorOptions.LOW: lambda crf_token: crf_token.text.lower(),
CRFEntityExtractorOptions.TITLE: lambda crf_token: crf_token.text.istitle(),
CRFEntityExtractorOptions.PREFIX5: lambda crf_token: crf_token.text[:5],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@

import numpy as np
import scipy.sparse
from sklearn.exceptions import NotFittedError
from sklearn.feature_extraction.text import CountVectorizer

import rasa.shared.utils.io
from rasa.engine.graph import GraphComponent, ExecutionContext
from rasa.engine.recipes.default_recipe import DefaultV1Recipe
from rasa.engine.storage.resource import Resource
from rasa.engine.storage.storage import ModelStorage
from rasa.nlu.featurizers.sparse_featurizer.sparse_featurizer import SparseFeaturizer
from rasa.nlu.utils.spacy_utils import SpacyModel
from rasa.shared.constants import DOCS_URL_COMPONENTS
import rasa.utils.io as io_utils
from sklearn.feature_extraction.text import CountVectorizer
from rasa.shared.nlu.training_data.training_data import TrainingData
from rasa.shared.nlu.training_data.message import Message
from rasa.shared.exceptions import RasaException, FileIOException
from rasa.nlu.constants import (
TOKENS_NAMES,
MESSAGE_ATTRIBUTES,
Expand Down
10 changes: 5 additions & 5 deletions rasa/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import filecmp
import logging
import os
import re
import tempfile
import warnings
import re
from asyncio import AbstractEventLoop
from pathlib import Path
from typing import Text, Any, Union, List, Type, Callable, TYPE_CHECKING, Pattern
from typing import Text, Any, List, Type, Callable, TYPE_CHECKING, Pattern

from typing_extensions import Protocol

import rasa.shared.constants
Expand Down Expand Up @@ -100,7 +101,6 @@ def create_temporary_directory() -> Text:

def create_path(file_path: Text) -> None:
"""Makes sure all directories in the 'file_path' exists."""

parent_dir = os.path.dirname(os.path.abspath(file_path))
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
Expand Down Expand Up @@ -136,8 +136,8 @@ def create_validator(
function: Callable[[Text], bool], error_message: Text
) -> Type["Validator"]:
"""Helper method to create `Validator` classes from callable functions. Should be
removed when questionary supports `Validator` objects."""

removed when questionary supports `Validator` objects.
"""
from prompt_toolkit.validation import Validator, ValidationError
from prompt_toolkit.document import Document

Expand Down
12 changes: 7 additions & 5 deletions rasa/utils/tensorflow/feature_array.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Dict, Any, List, Tuple, Optional, Union

from safetensors.numpy import save_file
import numpy as np
from safetensors.numpy import load_file
import scipy.sparse
from safetensors.numpy import load_file
from safetensors.numpy import save_file

import rasa.shared.utils.io

Expand Down Expand Up @@ -77,9 +77,9 @@ def _serialize_nested_data(
array_metadata["features"],
)

inner_metadata["components"].append(
inner_metadata["components"].append( # type:ignore[attr-defined]
array_metadata
) # type:ignore[attr-defined]
)

metadata.append(inner_metadata)

Expand Down Expand Up @@ -236,7 +236,9 @@ def __array_finalize__(self, obj: Optional[np.ndarray]) -> None:
return

self.units = getattr(obj, "units", None)
self.number_of_dimensions = getattr(obj, "number_of_dimensions", None) # type: ignore[assignment]
self.number_of_dimensions = getattr(
obj, "number_of_dimensions", None
) # type: ignore[assignment]
self.is_sparse = getattr(obj, "is_sparse", None)

default_attributes = {
Expand Down
2 changes: 0 additions & 2 deletions tests/utils/test_io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pathlib import Path

import pytest
from _pytest.tmpdir import TempPathFactory
from prompt_toolkit.document import Document
Expand Down

0 comments on commit 143a222

Please sign in to comment.