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

Detect whether a method is abstract or always raise an exception #860

Open
tristanlatr opened this issue Dec 13, 2024 · 1 comment
Open
Labels
astbuilder A substantive change is required in the astbuilder flow in order to fix this issue enhancement

Comments

@tristanlatr
Copy link
Contributor

Abstract methods and methods unconditionally raising exceptions should be handled specially because
we should not invite developers to use them directly.
This can be done by adding a special note for instance.

Also, these methods should not be shown in the constructors list on the header of class pages.

Examples of method that should be considered abstract:

# pydoctor should not care how the meta class is added since 
# it has no effect without decorated functions and the decorator expression is all new need.
class C: # Explicitly abstract class
	# Explicitly abstract methods
	import abc
	@abc.abstractmethod
	def f(): 
	@abc.abstractproperty
	def g(): 
	
	# Methods that does not do anything
	def h(): 
		"docs"
		...
	def i(): 
		"docs"
	def j(): ...
	def k(): pass
	
	# Methods that always raise the NotImplementedError
	def l(): raise NotImplementedError
	def m(): raise NotImplementedError()

class B(C):
	# Implicitely abstract class since the explicitly needed method "g"
	# is not implemented
	f,_ = *iter([)

class D(C):
	# Concrete class! Even if the "I" is missing, it's not an
	# explicitly needed method so pydoctor keeps it conservative.
	f,g,h,_,j,k,l,m = *iter([])
	

Mypy implemementation: https://github.com/python/mypy/blob/v1.13.0/mypy/semanal.py#L7550
We can do roughly the same.

Other interesting mypy code fragments:

https://github.com/python/mypy/blob/52888aec43ef8ba59645c7cd3ff5725ff9a861d7/mypy/checkexpr.py#L6234

https://github.com/python/mypy/blob/52888aec43ef8ba59645c7cd3ff5725ff9a861d7/mypy/semanal_classprop.py#L42

@tristanlatr tristanlatr added enhancement astbuilder A substantive change is required in the astbuilder flow in order to fix this issue labels Dec 13, 2024
@tristanlatr
Copy link
Contributor Author

tristanlatr commented Dec 13, 2024

Also instead of showing « overridden in » we should say « implemented in »

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astbuilder A substantive change is required in the astbuilder flow in order to fix this issue enhancement
Projects
None yet
Development

No branches or pull requests

1 participant