Repository: pydantic/pydantic

Issue:
ClassVar inheritance
### Checks

* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this feature/change is needed
* [x] After submitting this, I commit to one of:
    * Look through open issues and helped at least one other person
    * Hit the "watch" button on this repo to receive notifications and I commit to help at least 2 people that ask questions in the future
    * Implement a Pull Request for a confirmed bug

<!-- Sorry to sound so draconian, but every second saved replying to issues is time spend improving pydantic :-) -->

# Feature Request

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.7
            pydantic compiled: False
                 install path: [...]/pydantic
               python version: 3.9.0 (default, Oct  8 2020, 06:18:59)  [Clang 7.1.0 (tags/RELEASE_710/final)]
                     platform: macOS-10.14.6-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

```
<!-- or if you're using pydantic prior to v1.3, manually include: OS, python version and pydantic version -->

<!-- Please read the [docs](https://pydantic-docs.helpmanual.io/) and search through issues to
confirm your feature hasn't been asked for before, or already implemented. -->

<!-- Where possible please include a self-contained code snippet describing your feature request: -->

Currently, it is possible to annotate an attribute as being a class variable and it will be excluded from the model:

```py
from typing import ClassVar

from pydantic import BaseModel


class Foo(BaseModel):
    class_var_to_be_redefined_on_children: ClassVar[str] = "value"


class Bar(Foo):
    class_var_to_be_redefined_on_children = "some other value"

```

However, when inheriitng from a model with a `ClassVar`, overwriting the class variable produces an error:

```
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    class Bar(Foo):
  File "pydantic/main.py", line 284, in __new__
    validate_field_name(bases, var_name)
  File "utils.py", line 144, in validate_field_name
    raise NameError(
NameError: Field name "class_var_to_be_redefined_on_children" shadows a BaseModel attribute; use a different field name with "alias='class_var_to_be_redefined_on_children'".
```

What appears to be happening here is that Pydantic assumes that `class_var_to_be_redefined_on_children` is a Pydantic field but errors out because there is a class attribute with the same name.  I would prefer not to re-annotate subclass class variables with `ClassVar[T]`.  I believe the following should also be interpret as a `ClassVar`, especially in conjunction with generic models (an uninitialised `ClassVar`):

```py
from typing import ClassVar

from pydantic import BaseModel


class Foo(BaseModel):
    class_var_to_be_defined_on_children: ClassVar[str]


class Bar(Foo):
    class_var_to_be_defined_on_children = "some other value"
```
ClassVar inheritance
### Checks

* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this feature/change is needed
* [x] After submitting this, I commit to one of:
    * Look through open issues and helped at least one other person
    * Hit the "watch" button on this repo to receive notifications and I commit to help at least 2 people that ask questions in the future
    * Implement a Pull Request for a confirmed bug

<!-- Sorry to sound so draconian, but every second saved replying to issues is time spend improving pydantic :-) -->

# Feature Request

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.7
            pydantic compiled: False
                 install path: [...]/pydantic
               python version: 3.9.0 (default, Oct  8 2020, 06:18:59)  [Clang 7.1.0 (tags/RELEASE_710/final)]
                     platform: macOS-10.14.6-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

```
<!-- or if you're using pydantic prior to v1.3, manually include: OS, python version and pydantic version -->

<!-- Please read the [docs](https://pydantic-docs.helpmanual.io/) and search through issues to
confirm your feature hasn't been asked for before, or already implemented. -->

<!-- Where possible please include a self-contained code snippet describing your feature request: -->

Currently, it is possible to annotate an attribute as being a class variable and it will be excluded from the model:

```py
from typing import ClassVar

from pydantic import BaseModel


class Foo(BaseModel):
    class_var_to_be_redefined_on_children: ClassVar[str] = "value"


class Bar(Foo):
    class_var_to_be_redefined_on_children = "some other value"

```

However, when inheriitng from a model with a `ClassVar`, overwriting the class variable produces an error:

```
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    class Bar(Foo):
  File "pydantic/main.py", line 284, in __new__
    validate_field_name(bases, var_name)
  File "utils.py", line 144, in validate_field_name
    raise NameError(
NameError: Field name "class_var_to_be_redefined_on_children" shadows a BaseModel attribute; use a different field name with "alias='class_var_to_be_redefined_on_children'".
```

What appears to be happening here is that Pydantic assumes that `class_var_to_be_redefined_on_children` is a Pydantic field but errors out because there is a class attribute with the same name.  I would prefer not to re-annotate subclass class variables with `ClassVar[T]`.  I believe the following should also be interpret as a `ClassVar`, especially in conjunction with generic models (an uninitialised `ClassVar`):

```py
from typing import ClassVar

from pydantic import BaseModel


class Foo(BaseModel):
    class_var_to_be_defined_on_children: ClassVar[str]


class Bar(Foo):
    class_var_to_be_defined_on_children = "some other value"
```


Discussion:

Resolving patch:
diff --git a/pydantic/main.py b/pydantic/main.py
--- a/pydantic/main.py
+++ b/pydantic/main.py
@@ -16,7 +16,6 @@
     List,
     Mapping,
     Optional,
-    Set,
     Tuple,
     Type,
     TypeVar,
@@ -216,8 +215,9 @@ def __new__(mcs, name, bases, namespace, **kwargs):  # noqa C901
 
         pre_root_validators, post_root_validators = [], []
         private_attributes: Dict[str, ModelPrivateAttr] = {}
-        slots: Set[str] = namespace.get('__slots__', ())
+        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:
@@ -227,6 +227,7 @@ def __new__(mcs, name, bases, namespace, **kwargs):  # noqa C901
                 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 = inherit_config(namespace.get('Config'), config)
         validators = inherit_validators(extract_validators(namespace), validators)
@@ -242,7 +243,6 @@ def __new__(mcs, name, bases, namespace, **kwargs):  # noqa C901
 
         prepare_config(config, name)
 
-        class_vars = set()
         if (namespace.get('__module__'), namespace.get('__qualname__')) != ('pydantic.main', 'BaseModel'):
             annotations = resolve_annotations(namespace.get('__annotations__', {}), namespace.get('__module__', None))
             untouched_types = UNTOUCHED_TYPES + config.keep_untouched
@@ -318,6 +318,7 @@ def __new__(mcs, name, bases, namespace, **kwargs):  # noqa C901
             '__custom_root_type__': _custom_root_type,
             '__private_attributes__': private_attributes,
             '__slots__': slots | private_attributes.keys(),
+            '__class_vars__': class_vars,
             **{n: v for n, v in namespace.items() if n not in exclude_from_namespace},
         }
 
@@ -344,6 +345,7 @@ class BaseModel(Representation, metaclass=ModelMetaclass):
         __custom_root_type__: bool = False
         __signature__: 'Signature'
         __private_attributes__: Dict[str, Any]
+        __class_vars__: SetStr
         __fields_set__: SetStr = set()
 
     Config = BaseConfig


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -908,6 +908,12 @@ class MyModel(BaseModel):
 
     assert list(MyModel.__fields__.keys()) == ['c']
 
+    class MyOtherModel(MyModel):
+        a = ''
+        b = 2
+
+    assert list(MyOtherModel.__fields__.keys()) == ['c']
+
 
 def test_fields_set():
     class MyModel(BaseModel):