# Buggy code
def __new__(mcs, name, bases, namespace, **kwargs):  # noqa C901
    fields: Dict[str, ModelField] = {}
    config = BaseConfig
    validators: "ValidatorListDict" = {}

    pre_root_validators, post_root_validators = [], []
    private_attributes: Dict[str, ModelPrivateAttr] = {}
    slots: SetStr = namespace.get("__slots__", ())
    slots = {slots} if isinstance(slots, str) else set(slots)
    class_vars: SetStr = set()

    for base in reversed(bases):
        if (
            _is_base_model_class_defined
            and issubclass(base, BaseModel)
            and base != BaseModel
        ):
            fields.update(smart_deepcopy(base.__fields__))
            config = inherit_config(base.__config__, config)
            validators = inherit_validators(base.__validators__, validators)
            pre_root_validators += base.__pre_root_validators__
            post_root_validators += base.__post_root_validators__
            private_attributes.update(base.__private_attributes__)
            class_vars.update(base.__class_vars__)

    config_kwargs = {
        key: kwargs.pop(key) for key in kwargs.keys() & BaseConfig.__dict__.keys()
    }
    config_from_namespace = namespace.get("Config")
    if config_kwargs and config_from_namespace:
        raise TypeError(
            "Specifying config in two places is ambiguous, use either Config attribute or class kwargs"
        )
    config = inherit_config(config_from_namespace, config, **config_kwargs)

    validators = inherit_validators(extract_validators(namespace), validators)
    vg = ValidatorGroup(validators)

    for f in fields.values():
        f.set_config(config)
        extra_validators = vg.get_validators(f.name)
        if extra_validators:
            f.class_validators.update(extra_validators)
            # re-run prepare to add extra validators
            f.populate_validators()

    prepare_config(config, name)

    untouched_types = ANNOTATED_FIELD_UNTOUCHED_TYPES

    def is_untouched(v: Any) -> bool:
        return (
            isinstance(v, untouched_types)
            or v.__class__.__name__ == "cython_function_or_method"
        )

    if (namespace.get("__module__"), namespace.get("__qualname__")) != (
        "pydantic.main",
        "BaseModel",
    ):
        annotations = resolve_annotations(
            namespace.get("__annotations__", {}), namespace.get("__module__", None)
        )
        # annotation only fields need to come first in fields
        for ann_name, ann_type in annotations.items():
            if is_classvar(ann_type):
                class_vars.add(ann_name)
            elif is_valid_field(ann_name):
                validate_field_name(bases, ann_name)
                value = namespace.get(ann_name, Undefined)
                allowed_types = (
                    get_args(ann_type) if get_origin(ann_type) is Union else (ann_type,)
                )
                if (
                    is_untouched(value)
                    and ann_type != PyObject
                    and not any(
                        lenient_issubclass(get_origin(allowed_type), Type)
                        for allowed_type in allowed_types
                    )
                ):
                    continue
                fields[ann_name] = ModelField.infer(
                    name=ann_name,
                    value=value,
                    annotation=ann_type,
                    class_validators=vg.get_validators(ann_name),
                    config=config,
                )
            elif ann_name not in namespace and config.underscore_attrs_are_private:
                private_attributes[ann_name] = PrivateAttr()

        untouched_types = UNTOUCHED_TYPES + config.keep_untouched
        for var_name, value in namespace.items():
            can_be_changed = var_name not in class_vars and not is_untouched(value)
            if isinstance(value, ModelPrivateAttr):
                if not is_valid_private_name(var_name):
                    raise NameError(
                        f'Private attributes "{var_name}" must not be a valid field name; '
                        f'Use sunder or dunder names, e. g. "_{var_name}" or "__{var_name}__"'
                    )
                private_attributes[var_name] = value
            elif (
                config.underscore_attrs_are_private
                and is_valid_private_name(var_name)
                and can_be_changed
            ):
                private_attributes[var_name] = PrivateAttr(default=value)
            elif (
                is_valid_field(var_name)
                and var_name not in annotations
                and can_be_changed
            ):
                validate_field_name(bases, var_name)
                inferred = ModelField.infer(
                    name=var_name,
                    value=value,
                    annotation=annotations.get(var_name, Undefined),
                    class_validators=vg.get_validators(var_name),
                    config=config,
                )
                if var_name in fields and inferred.type_ != fields[var_name].type_:
                    raise TypeError(
                        f"The type of {name}.{var_name} differs from the new default value; "
                        f"if you wish to change the type of this field, please use a type annotation"
                    )
                fields[var_name] = inferred

    _custom_root_type = ROOT_KEY in fields
    if _custom_root_type:
        validate_custom_root_type(fields)
    vg.check_for_unused()
    if config.json_encoders:
        json_encoder = partial(custom_pydantic_encoder, config.json_encoders)
    else:
        json_encoder = pydantic_encoder
    pre_rv_new, post_rv_new = extract_root_validators(namespace)

    exclude_from_namespace = fields | private_attributes.keys() | {"__slots__"}
    new_namespace = {
        "__config__": config,
        "__fields__": fields,
        "__validators__": vg.validators,
        "__pre_root_validators__": unique_list(pre_root_validators + pre_rv_new),
        "__post_root_validators__": unique_list(post_root_validators + post_rv_new),
        "__schema_cache__": {},
        "__json_encoder__": staticmethod(json_encoder),
        "__custom_root_type__": _custom_root_type,
        "__private_attributes__": private_attributes,
        "__slots__": slots | private_attributes.keys(),
        "__hash__": generate_hash_function(config.frozen),
        "__class_vars__": class_vars,
        **{n: v for n, v in namespace.items() if n not in exclude_from_namespace},
    }

    cls = super().__new__(mcs, name, bases, new_namespace, **kwargs)
    # set __signature__ attr only for model class, but not for its instances
    cls.__signature__ = ClassAttribute(
        "__signature__", generate_model_signature(cls.__init__, fields, config)
    )
    return cls


# Traceback (ValueError)
Traceback (most recent call last):
File "bug.py", line 4, in <module>
class Foo1(BaseModel):
File "pydantic/main.py", line 352, in pydantic.main.ModelMetaclass.__new__
File "/usr/lib/python3.8/abc.py", line 85, in __new__
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
ValueError: '__hash__' in __slots__ conflicts with class variable

# Fixed code
def __new__(mcs, name, bases, namespace, **kwargs):  # noqa C901
    fields: Dict[str, ModelField] = {}
    config = BaseConfig
    validators: "ValidatorListDict" = {}

    pre_root_validators, post_root_validators = [], []
    private_attributes: Dict[str, ModelPrivateAttr] = {}
    slots: SetStr = namespace.get("__slots__", ())
    slots = {slots} if isinstance(slots, str) else set(slots)
    class_vars: SetStr = set()
    hash_func: Optional[Callable[[Any], int]] = None

    for base in reversed(bases):
        if (
            _is_base_model_class_defined
            and issubclass(base, BaseModel)
            and base != BaseModel
        ):
            fields.update(smart_deepcopy(base.__fields__))
            config = inherit_config(base.__config__, config)
            validators = inherit_validators(base.__validators__, validators)
            pre_root_validators += base.__pre_root_validators__
            post_root_validators += base.__post_root_validators__
            private_attributes.update(base.__private_attributes__)
            class_vars.update(base.__class_vars__)
            hash_func = base.__hash__

    config_kwargs = {
        key: kwargs.pop(key) for key in kwargs.keys() & BaseConfig.__dict__.keys()
    }
    config_from_namespace = namespace.get("Config")
    if config_kwargs and config_from_namespace:
        raise TypeError(
            "Specifying config in two places is ambiguous, use either Config attribute or class kwargs"
        )
    config = inherit_config(config_from_namespace, config, **config_kwargs)

    validators = inherit_validators(extract_validators(namespace), validators)
    vg = ValidatorGroup(validators)

    for f in fields.values():
        f.set_config(config)
        extra_validators = vg.get_validators(f.name)
        if extra_validators:
            f.class_validators.update(extra_validators)
            # re-run prepare to add extra validators
            f.populate_validators()

    prepare_config(config, name)

    untouched_types = ANNOTATED_FIELD_UNTOUCHED_TYPES

    def is_untouched(v: Any) -> bool:
        return (
            isinstance(v, untouched_types)
            or v.__class__.__name__ == "cython_function_or_method"
        )

    if (namespace.get("__module__"), namespace.get("__qualname__")) != (
        "pydantic.main",
        "BaseModel",
    ):
        annotations = resolve_annotations(
            namespace.get("__annotations__", {}), namespace.get("__module__", None)
        )
        # annotation only fields need to come first in fields
        for ann_name, ann_type in annotations.items():
            if is_classvar(ann_type):
                class_vars.add(ann_name)
            elif is_valid_field(ann_name):
                validate_field_name(bases, ann_name)
                value = namespace.get(ann_name, Undefined)
                allowed_types = (
                    get_args(ann_type) if get_origin(ann_type) is Union else (ann_type,)
                )
                if (
                    is_untouched(value)
                    and ann_type != PyObject
                    and not any(
                        lenient_issubclass(get_origin(allowed_type), Type)
                        for allowed_type in allowed_types
                    )
                ):
                    continue
                fields[ann_name] = ModelField.infer(
                    name=ann_name,
                    value=value,
                    annotation=ann_type,
                    class_validators=vg.get_validators(ann_name),
                    config=config,
                )
            elif ann_name not in namespace and config.underscore_attrs_are_private:
                private_attributes[ann_name] = PrivateAttr()

        untouched_types = UNTOUCHED_TYPES + config.keep_untouched
        for var_name, value in namespace.items():
            can_be_changed = var_name not in class_vars and not is_untouched(value)
            if isinstance(value, ModelPrivateAttr):
                if not is_valid_private_name(var_name):
                    raise NameError(
                        f'Private attributes "{var_name}" must not be a valid field name; '
                        f'Use sunder or dunder names, e. g. "_{var_name}" or "__{var_name}__"'
                    )
                private_attributes[var_name] = value
            elif (
                config.underscore_attrs_are_private
                and is_valid_private_name(var_name)
                and can_be_changed
            ):
                private_attributes[var_name] = PrivateAttr(default=value)
            elif (
                is_valid_field(var_name)
                and var_name not in annotations
                and can_be_changed
            ):
                validate_field_name(bases, var_name)
                inferred = ModelField.infer(
                    name=var_name,
                    value=value,
                    annotation=annotations.get(var_name, Undefined),
                    class_validators=vg.get_validators(var_name),
                    config=config,
                )
                if var_name in fields and inferred.type_ != fields[var_name].type_:
                    raise TypeError(
                        f"The type of {name}.{var_name} differs from the new default value; "
                        f"if you wish to change the type of this field, please use a type annotation"
                    )
                fields[var_name] = inferred

    _custom_root_type = ROOT_KEY in fields
    if _custom_root_type:
        validate_custom_root_type(fields)
    vg.check_for_unused()
    if config.json_encoders:
        json_encoder = partial(custom_pydantic_encoder, config.json_encoders)
    else:
        json_encoder = pydantic_encoder
    pre_rv_new, post_rv_new = extract_root_validators(namespace)

    if hash_func is None:
        hash_func = generate_hash_function(config.frozen)

    exclude_from_namespace = fields | private_attributes.keys() | {"__slots__"}
    new_namespace = {
        "__config__": config,
        "__fields__": fields,
        "__validators__": vg.validators,
        "__pre_root_validators__": unique_list(pre_root_validators + pre_rv_new),
        "__post_root_validators__": unique_list(post_root_validators + post_rv_new),
        "__schema_cache__": {},
        "__json_encoder__": staticmethod(json_encoder),
        "__custom_root_type__": _custom_root_type,
        "__private_attributes__": private_attributes,
        "__slots__": slots | private_attributes.keys(),
        "__hash__": hash_func,
        "__class_vars__": class_vars,
        **{n: v for n, v in namespace.items() if n not in exclude_from_namespace},
    }

    cls = super().__new__(mcs, name, bases, new_namespace, **kwargs)
    # set __signature__ attr only for model class, but not for its instances
    cls.__signature__ = ClassAttribute(
        "__signature__", generate_model_signature(cls.__init__, fields, config)
    )
    return cls