Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update constants import to use module-level access #1172 #2453

Merged
merged 6 commits into from
Aug 20, 2024

Conversation

WizKnight
Copy link
Contributor

@WizKnight WizKnight commented Aug 15, 2024

This PR addresses issue #1172 by updating the way constants are imported and used throughout the Hugging Face Hub codebase. Instead of directly importing specific constants, we now import the entire constants module and access constants as its attributes.

@WizKnight WizKnight changed the title Update constants import to use module-level access (#1172) Update constants import to use module-level access #1172 Aug 15, 2024
@Wauplin Wauplin self-requested a review August 16, 2024 11:03
Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @WizKnight, that's it yes! Let's wait for the CI to pass and then we are good to merge this one :)

@Wauplin
Copy link
Contributor

Wauplin commented Aug 16, 2024

@WizKnight code quality seems to be complaining. To fix this, you must run make style locally which will fix the issues. Then you can run make quality to check everything's good. Finally, commit and push the changes.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Comment on lines 84 to 104
from .constants import (
DEFAULT_ETAG_TIMEOUT,
DEFAULT_REQUEST_TIMEOUT,
DEFAULT_REVISION,
DISCUSSION_STATUS,
DISCUSSION_TYPES,
ENDPOINT,
INFERENCE_ENDPOINTS_ENDPOINT,
REGEX_COMMIT_OID,
REPO_TYPE_MODEL,
REPO_TYPES,
REPO_TYPES_MAPPING,
REPO_TYPES_URL_PREFIXES,
SAFETENSORS_INDEX_FILE,
SAFETENSORS_MAX_HEADER_LENGTH,
SAFETENSORS_SINGLE_FILE,
SPACES_SDK_TYPES,
WEBHOOK_DOMAIN_T,
DiscussionStatusFilter,
DiscussionTypeFilter,
)
Copy link
Contributor

@Wauplin Wauplin Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WizKnight it seems this PR broke something in the tests. There are 178 failed test with the same error:

FAILED ../tests/test_file_download.py::TestNormalizeEtag::test_resolve_endpoint_on_regular_file - AttributeError: <module 'huggingface_hub.hf_api' from '/home/runner/work/huggingface_hub/huggingface_hub/src/huggingface_hub/hf_api.py'> does not have the attribute 'ENDPOINT'

Makes me realize we should keep all those previously existing constants in the module even if we don't use them. You can do that by adding

# noqa: F401 # kept for backward compatibility

to each of them. Since huggingface_hub.hf_api is "public", we can't assume that no-one in the ecosystem has imported a constant from this module. It's unfortunately a bit ugly but we have to do that for backward compatibility.

Copy link
Contributor Author

@WizKnight WizKnight Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Wauplin Yeah, it seems so and all those tests have just one common AttributeError: 'ENDPOINT'. I worked on those failed tests, I got it down to 94 failed, 189 passed, 6 skipped, 3 errors.
Though for make quality: mypy src
Success: no issues found in 106 source files

So now how should I proceed hereafter??

Copy link
Contributor Author

@WizKnight WizKnight Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Wauplin All the tests passed!!! Just for ENDPOINT I reverted back to the previous import. It resolved the issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that we should keep the imports with

from .constants import (
    DEFAULT_ETAG_TIMEOUT,  # noqa: F401 # kept for backward compatibility
    DEFAULT_REQUEST_TIMEOUT,  # noqa: F401 # kept for backward compatibility
    DEFAULT_REVISION,  # noqa: F401 # kept for backward compatibility
    DISCUSSION_STATUS,  # noqa: F401 # kept for backward compatibility
    DISCUSSION_TYPES,  # noqa: F401 # kept for backward compatibility
    ENDPOINT,  # noqa: F401 # kept for backward compatibility
    INFERENCE_ENDPOINTS_ENDPOINT,  # noqa: F401 # kept for backward compatibility
    REGEX_COMMIT_OID,  # noqa: F401 # kept for backward compatibility
    REPO_TYPE_MODEL,  # noqa: F401 # kept for backward compatibility
    REPO_TYPES,  # noqa: F401 # kept for backward compatibility
    REPO_TYPES_MAPPING,  # noqa: F401 # kept for backward compatibility
    REPO_TYPES_URL_PREFIXES,  # noqa: F401 # kept for backward compatibility
    SAFETENSORS_INDEX_FILE,  # noqa: F401 # kept for backward compatibility
    SAFETENSORS_MAX_HEADER_LENGTH,  # noqa: F401 # kept for backward compatibility
    SAFETENSORS_SINGLE_FILE,  # noqa: F401 # kept for backward compatibility
    SPACES_SDK_TYPES,  # noqa: F401 # kept for backward compatibility
    WEBHOOK_DOMAIN_T,  # noqa: F401 # kept for backward compatibility
    DiscussionStatusFilter,  # noqa: F401 # kept for backward compatibility
    DiscussionTypeFilter,  # noqa: F401 # kept for backward compatibility
)

Ugly? yes. But backward compatible and might save us some headaches.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll add backward compatibility to the imports

Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @WizKnight, I've made a new review. I'm not sure about the purpose of most of the new changes. It seems that you had some issues with your local setup. I'd advice you to install and upgrade all dev dependencies in a new virtual environment. If you have any question, please let me know.

Makefile Outdated
Comment on lines 10 to 13
python3 utils/check_inference_input_params.py
python3 utils/check_contrib_list.py
python3 utils/check_static_imports.py
python3 utils/generate_async_inference_client.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes should not be committed. If you struggle with python version, I advice you to use a virtual environment. See installation guide for more details: https://huggingface.co/docs/huggingface_hub/installation#install-with-pip

Copy link
Contributor Author

@WizKnight WizKnight Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using virtual environment with python 3.10 and all the required dependencies installed.
Sure I'll fix the changes

Makefile Outdated
Comment on lines 19 to 22
python3 utils/check_contrib_list.py --update
python3 utils/check_static_imports.py --update
python3 utils/generate_async_inference_client.py --update
ruff format src/huggingface_hub/hf_api.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here

@@ -143,14 +143,11 @@
"CommitOperationAdd",
"CommitOperationCopy",
"CommitOperationDelete",
"DatasetInfo",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these objects must not be removed from __init__.py (ModelInfo, SpaceInfo, get_user_overview, ... as well). Can you put them back? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will


# Optimize operations: if some files will be overwritten, we don't need to delete them first
if len(add_operations) > 0:
added_paths = set(op.path_in_repo for op in add_operations)
delete_operations = [
delete_op for delete_op in delete_operations if delete_op.path_in_repo not in added_paths
]
commit_operations = delete_operations + add_operations
commit_operations = cast(List[CommitOperation], delete_operations) + cast(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why that? If it's because of an issue when running make quality locally, then you might not have the correct requirements installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll redo it from scratch. Even after that if problem persists, I'll inform you.

Comment on lines 4873 to 4874
# Assuming CommitOperationBase is the common base class
CommitOperation = Union[CommitOperationAdd, CommitOperationDelete]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Comment on lines 556 to 563
if is_dataclass(cls):
for key in cls.__dataclass_fields__:
if is_dataclass(cls) and isinstance(cls, type):
# Get type hints for the class
cls_hints = get_type_hints(cls)
if "__dataclass_fields__" in cls_hints:
for key in cls_hints["__dataclass_fields__"]:
if key not in model_kwargs and key in config:
model_kwargs[key] = config[key]
elif any(param.kind == inspect.Parameter.VAR_KEYWORD for param in cls._hub_mixin_init_parameters.values()):
for key, value in config.items():
if key not in model_kwargs:
model_kwargs[key] = value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is to fix an unrelated issue, better to open a separate PR.

Copy link
Contributor Author

@WizKnight WizKnight Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was for the Error: Module "_typeshed" has no attribute "DataclassInstance" &
Error: "Type[T]" has no attribute "__dataclass_fields__"

@@ -92,21 +92,23 @@ def WeakFileLock(lock_file: Union[str, Path]) -> Generator[BaseFileLock, None, N

An INFO log message is emitted every 10 seconds if the lock is not acquired immediately.
"""
lock = FileLock(lock_file, timeout=constants.FILELOCK_LOG_EVERY_SECONDS)
lock_file_str = str(lock_file) # Convert to string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this:
src/huggingface_hub/utils/_fixes.py:95: error: Argument 1 to "UnixFileLock" has incompatible type "Union[str, Path]"; expected "str"
src/huggingface_hub/utils/_fixes.py:108: error: Incompatible types in assignment (expression has type "SoftFileLock", variable has type "UnixFileLock")
src/huggingface_hub/utils/_fixes.py:108: error: Argument 1 to "SoftFileLock" has incompatible type "Union[str, Path]"; expected "str"

Comment on lines 84 to 104
from .constants import (
DEFAULT_ETAG_TIMEOUT,
DEFAULT_REQUEST_TIMEOUT,
DEFAULT_REVISION,
DISCUSSION_STATUS,
DISCUSSION_TYPES,
ENDPOINT,
INFERENCE_ENDPOINTS_ENDPOINT,
REGEX_COMMIT_OID,
REPO_TYPE_MODEL,
REPO_TYPES,
REPO_TYPES_MAPPING,
REPO_TYPES_URL_PREFIXES,
SAFETENSORS_INDEX_FILE,
SAFETENSORS_MAX_HEADER_LENGTH,
SAFETENSORS_SINGLE_FILE,
SPACES_SDK_TYPES,
WEBHOOK_DOMAIN_T,
DiscussionStatusFilter,
DiscussionTypeFilter,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that we should keep the imports with

from .constants import (
    DEFAULT_ETAG_TIMEOUT,  # noqa: F401 # kept for backward compatibility
    DEFAULT_REQUEST_TIMEOUT,  # noqa: F401 # kept for backward compatibility
    DEFAULT_REVISION,  # noqa: F401 # kept for backward compatibility
    DISCUSSION_STATUS,  # noqa: F401 # kept for backward compatibility
    DISCUSSION_TYPES,  # noqa: F401 # kept for backward compatibility
    ENDPOINT,  # noqa: F401 # kept for backward compatibility
    INFERENCE_ENDPOINTS_ENDPOINT,  # noqa: F401 # kept for backward compatibility
    REGEX_COMMIT_OID,  # noqa: F401 # kept for backward compatibility
    REPO_TYPE_MODEL,  # noqa: F401 # kept for backward compatibility
    REPO_TYPES,  # noqa: F401 # kept for backward compatibility
    REPO_TYPES_MAPPING,  # noqa: F401 # kept for backward compatibility
    REPO_TYPES_URL_PREFIXES,  # noqa: F401 # kept for backward compatibility
    SAFETENSORS_INDEX_FILE,  # noqa: F401 # kept for backward compatibility
    SAFETENSORS_MAX_HEADER_LENGTH,  # noqa: F401 # kept for backward compatibility
    SAFETENSORS_SINGLE_FILE,  # noqa: F401 # kept for backward compatibility
    SPACES_SDK_TYPES,  # noqa: F401 # kept for backward compatibility
    WEBHOOK_DOMAIN_T,  # noqa: F401 # kept for backward compatibility
    DiscussionStatusFilter,  # noqa: F401 # kept for backward compatibility
    DiscussionTypeFilter,  # noqa: F401 # kept for backward compatibility
)

Ugly? yes. But backward compatible and might save us some headaches.

WizKnight

This comment was marked as resolved.

@Wauplin
Copy link
Contributor

Wauplin commented Aug 20, 2024

Hi @WizKnight, I've checked out locally your changes to test typing issues you were having. I can't reproduce them and the CI either. I suspect that we are not on the same version (currently using mypy==1.5.1 in CI). For now I reverted the typing-related changes from this PR (see 62af016). I'll have a look to what you mention though but keeping it for a separate one.

CI is now passing except for unrelated errors. Time to merge! Thanks again for the contribution!

@Wauplin Wauplin merged commit 1b9517d into huggingface:main Aug 20, 2024
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants